Wormholes (poj 3259 SPFA | Bellman_Ford negative ring), wormholespoj
Language:DefaultWormholes
Time Limit:2000 MS |
|
Memory Limit:65536 K |
Total Submissions:33291 |
|
Accepted:12130 |
Description While processing his own farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprisesN(1 ≤N≤ 500) fields conveniently numbered 1 ..N,M(1 ≤M≤ 2500) paths, andW(1 ≤W≤ 200) wormholes. As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. perhaps he will be able to meet himself :). To help FJ find out whether this is possible or not, he will supply you with complete mapsF(1 ≤F≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds. Input Line 1: A single integer,F.FFarm descriptions follow. Line 1 of each farm: Three space-separated integers respectively:N,M, AndW Lines 2 ..M+ 1 of each farm: Three space-separated numbers (S,E,T) That describe, respectively: a bidirectional pathSAndEThat requiresTSeconds to traverse. Two fields might be connected by more than one path. LinesM+ 2 ..M+W+ 1 of each farm: Three space-separated numbers (S,E,T) That describe, respectively: A one way path fromSToEThat also moves the traveler backTSeconds.Output Lines 1 ..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes ).Sample Input 23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8 Sample Output NOYES Hint For farm 1, FJ cannot travel back in time. For farm 2, FJ cocould travel back in time by the cycle 1-> 2-> 3-> 1, arriving back at his starting location 1 second before he leaves. he cocould start from anywhere on the cycle to accomplish this.Source USACO 2006 December Gold |
John's farm has a field and a path connecting two places. The hole is a wormhole hole, which is a one-way path that not only transfers you to the destination, and the time will go backwards. Our task is to know if we will return again after starting from a certain place and see ourselves before leaving.
I wrote the short circuit a long time ago and reviewed SPFA and Bellma_Ford to judge the negative weight loop.
Code:
//Bellman_Ford#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 550#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b) for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b) for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v) memset ((t) , v, sizeof(t))#define sf(n) scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf printf#define DBG pf("Hi\n")typedef long long ll;using namespace std;struct Edge{ int u,v,w;}edge[maxn*10];int n,m,w,num;int dist[maxn];bool Bellman_Ford(){ int i,j; FRL(i,0,n+2) dist[i]=INF; dist[1]=0; FRL(i,1,n) { bool flag=false; FRL(j,0,num) { int u=edge[j].u; int v=edge[j].v; if (dist[v]>dist[u]+edge[j].w) { flag=true; dist[v]=dist[u]+edge[j].w; } } if (!flag) return true; } FRL(i,0,num) if (dist[ edge[i].v ]>dist[ edge[i].u ]+edge[i].w) return false; return true;}int main(){ int i,j,cas; sf(cas); while (cas--) { num=0; int u,v,t; sfff(n,m,w); FRL(i,0,m) { sfff(u,v,t); edge[num].u=u; edge[num].v=v; edge[num].w=t; num++; edge[num].u=v; edge[num].v=u; edge[num].w=t; num++; } FRL(i,0,w) { sfff(u,v,t); edge[num].u=u; edge[num].v=v; edge[num].w=-t; num++; } if (Bellman_Ford()) pf("NO\n"); else pf("YES\n"); } return 0;}
//SPFA#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 550#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b) for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b) for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v) memset ((t) , v, sizeof(t))#define sf(n) scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf printf#define DBG pf("Hi\n")typedef long long ll;using namespace std;struct Edge{ int v,w; int next;}edge[maxn*10];int n,m,w,num;int head[maxn];int dist[maxn];bool inq[maxn];int cnt[maxn];void init(){ num=0; mem(head,-1); mem(inq,false); mem(cnt,0); mem(dist,INF);}void addedge(int u,int v,int w){ edge[num].v=v; edge[num].w=w; edge[num].next=head[u]; head[u]=num++;}bool SPFA(){ queue<int>Q; inq[1]=true; cnt[1]=1; dist[1]=0; while (!Q.empty()) Q.pop(); Q.push(1); while (!Q.empty()) { int u=Q.front(); Q.pop(); inq[u]=false; if (cnt[u]>n) return false; for (int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if (dist[v]>dist[u]+edge[i].w) { dist[v]=dist[u]+edge[i].w; if (!inq[v]) { inq[v]=true; cnt[v]++; Q.push(v); } } } } return true;}int main(){ int i,j,cas; sf(cas); while (cas--) { int u,v,t; init(); sfff(n,m,w); FRL(i,0,m) { sfff(u,v,t); addedge(u,v,t); addedge(v,u,t); } FRL(i,0,w) { sfff(u,v,t); addedge(u,v,-t); } if (SPFA()) pf("NO\n"); else pf("YES\n"); } return 0;}