Topic Links:
http://poj.org/problem?id=3259
Main topic:
There is a farmer through time, he has n farms, each farm has m two-way road, there are w one-way wormhole, each road has three attributes (S,e,t) respectively represents the time from S to E to spend T, each wormhole also has three properties (S,e,t), respectively, represents the S arrived e to spend-t time, The farmer wants to meet his dream of traveling through time, he wants to wander around his farm and go back to the original point and see the original himself, if he can meet his dream output "YES", no "no".
Problem Solving Ideas:
It can be seen that the problem is to judge whether there is a negative power circuit in the map, with SPFA can be solved.
1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <vector>5#include <queue>6#include <cmath>7#include <iostream>8#include <algorithm>9 using namespacestd;Ten #defineMAXN 510 One #defineINF 0x3f3f3f3f A - structEdge - { the inte, W; -Edge (intE=0,intw=0): E (E), W (w) {} - }; - +Vector<edge>G[MAXN]; - intDIST[MAXN], N; + voidinit (); A BOOLSPFA (); at - intMain () - { - intF; -scanf ("%d", &f); - while(F--) in { - intm, W, I; to init (); +scanf (" %d%d%d", &n, &m, &W); - the for(i=0; i<m; i++) * { $ intA, B, C;Panax Notoginsengscanf" %d%d%d", &a, &b, &c); - G[a].push_back (Edge (b, c)); the G[b].push_back (Edge (A, c)); + } A the for(i=0; i<w; i++) + { - intA, B, C; $scanf (" %d%d%d", &a, &b, &c); $G[a].push_back (Edge (b,-c)); - } - if(SPFA ()) theprintf ("no\n"); - ElseWuyiprintf ("yes\n"); the } - return 0; Wu } - About voidInit () $ { - for(intI=0; i<maxn; i++) -Dist[i] =INF, G[i].clear (); - } A + BOOLSPFA () the { - intUPDATA[MAXN]; $Queue<edge>que; the Edge PN; thePN.E =1, PN.W =0; thememset (Updata,0,sizeof(Updata)); the - Que.push (PN); indist[1] =0; theUPDATA[PN.E] =1; the while( !que.empty ()) About { thePN =Que.front (); the Que.pop (); the intLen =g[pn.e].size (); + - for(intI=0; i<len; i++) the {BayiEdge p =G[pn.e][i]; the if(DIST[P.E] > DIST[PN.E] +P.W) the { -DIST[P.E] = dist[pn.e] +P.W; - Que.push (p); theUPDATA[P.E] + +; the if(UPDATA[P.E] = =N) the return false; the } - } the } the return true; the}
POJ 3259 wormholes