您的位置:首页 > 其它

Hdu2437-Jerboas(取余数判重搜索)

2016-07-11 15:13 309 查看
Jerboas are small desert-living animals, which resemble mice with a long tufted tail and very long hind legs. Jerboas shelter in well-hidden burrows. They create two types of burrow: temporary and permanent. The temporary burrows are plain tubes while the permanent burrows are sealed with a plug of sand to keep heat out and moisture in.

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iterator>
#include<stack>
using namespace std;
const int INF=1e9+7;
const double eps=1e-7;
const int maxn=1005;
int N,M,start,mod;
bool vis[maxn][maxn];
char road[maxn];
struct node
{
int val,rest,pos;
node(int val=0,int rest=0,int pos=0):val(val),rest(rest),pos(pos){}
bool operator < (const node& t) const{ return val>t.val; }
};
priority_queue<node> que;
struct edge
{
int u,v,c;
edge(int u=0,int v=0,int c=0):u(u),v(v),c(c){}
};
vector<edge> G[maxn];
void init()
{
while(!que.empty()) que.pop();
for(int i=0;i<maxn;i++) G[i].clear();
memset(vis,false,sizeof(vis));
}
void solve()
{
int ansl=INF,ansp=-1;
que.push(node(0,0,start));
vis[0][start]=true;
while(!que.empty())
{
node t=que.top();  que.pop();
int val=t.val,rest=t.rest,pos=t.pos;
if(val>ansl) break; //路径长度大了
if(rest==0&&road[pos]=='P')  //是P点更新答案
{
if(val<ansl||(val==ansl&&pos<ansp))
{
ansl=val;
ansp=pos;
}
}
int Size=G[pos].size();
for(int i=0;i<Size;i++)
{
edge& e=G[pos][i];
int v=e.v,c=e.c+val;
if(vis[c%mod][v]) continue;
vis[c%mod][v]=true;
que.push(node(c,c%mod,v));
}
}
if(ansp==-1) printf("-1 -1\n");
else printf("%d %d\n",ansl,ansp);
}
int main()
{
int T,Case=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&N,&M,&start,&mod);
scanf("%s",road+1);   //输入
init();
int u,v,c;
while(M--)
{
scanf("%d%d%d",&u,&v,&c);
G[u].push_back(edge(u,v,c));//边
}
printf("Case %d: ",++Case);
solve();
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: