Hdu2437-Jerboas(取余数判重搜索)
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.
As far as we know, jerboa burrows in the desert are connected with one-way tunnels. What's more, for some unknown reasons, it's true that start from any burrow, follows the tunnels you can not go back to the starting burrow. Summer means last-minute of offers on good times, so of course jerboas could not stay behind. One day, a little jerboa Alice who lived in a temporary burrow S wants to migrate to a permanent one. There are different routes she can take, but Alice is so odd that she only selects those whose total travel distances is a multiple of K. Among all routes that Alice may select, we are interested in the shortest one. Can you help to find it out? Of course different routes may lead to different destinations.
Each test case starts with four integers in the first line: N, M, S, K. N is the number of burrows in the desert (burrows are numbered with 1, 2, …, N); M is the number of tunnels connecting the burrows;
S is where Alice lived and K is as described above. (0 < N <= 1000, 0 <= M <= 20000, 0 < S <= N, 0 < K <= 1000) The second line contains N characters each could be ‘T’ or ‘P’. The i-th character specifying the type of the burrow i. ‘T’ means temporary burrow, ‘P’ means permanent burrow. It’s guaranteed that the S-th character is ‘T’. Next follow M lines, each line with 3 integers A, B, C. Specifying that there is a tunnel from burrow A to burrow B, and its length is C. (0 < A, B <= N, A != B, 0 < C < 40000)
#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; }