Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 44174 | Accepted: 16235 |
Description
While exploring his many 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 comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (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 maps to F (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 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2..M+W+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
Output
Sample Input
2 3 3 1 1 2 2 1 3 4 2 3 1 3 1 3 3 2 1 1 2 3 2 3 4 3 1 8
Sample Output
NO YES
Hint
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.
题目大意:
一个农民有农场,上面有一些虫洞和路,走虫洞可以回到 T秒前,而路就和平常的一样啦,需要花费时间走过。问该农民可不可能从某个点出发后回到该点,并且于出发时间之前?
直接上spfa,看了很多模板,只有这个模板的命名规范些,以后就照这个模板来做吧。
题目大意:
一个农民有农场,上面有一些虫洞和路,走虫洞可以回到 T秒前,而路就和平常的一样啦,需要花费时间走过。问该农民可不可能从某个点出发后回到该点,并且于出发时间之前?
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 520 ; const int maxm = 5200; struct edge { int to; int val; int next; }e[maxm]; int len,head[maxn]; int dis[maxn]; int n,m,c; bool spfa() { for(int i = 1;i<=n;i++) dis[i]=inf; bool vis[maxn] = {0}; int cnt[maxn] = {0}; //因为这里是找环,不是找到某点的最短路径,所以不用初始dis[start]=0;直接从1开始找。 int cur = 1; //当前点初始化 queue<int> q; q.push(cur); vis[cur] = true; cnt[cur]=1; dis[cur]=0; //以当前点的为始点的最短路径 while(!q.empty()) { cur=q.front(); q.pop(); vis[cur]=false;//出队则取消标记 for(int i = head[cur];i!=-1;i=e[i].next) { int tto = e[i].to; if( dis[tto]>dis[cur]+e[i].val ) { dis[tto] = dis[cur]+e[i].val; if(!vis[tto]){ cnt[tto]++; vis[tto]=true; q.push(tto);//新点进队 if(cnt[cur]>n) return true;//如果进队超过n次,则存在负环 } } } } return false; } void add(int from,int to,int val){ e[len].to=to; e[len].val=val; e[len].next=head[from]; head[from]=len++; } int main(){ int T; scanf("%d",&T); while(T--){ memset(head,-1,sizeof(head)); len=0; scanf("%d%d%d",&n,&m,&c); for(int i = 0;i<m;i++){ int from,to,val; scanf("%d%d%d",&from,&to,&val); add(from,to,val); add(to,from,val); } for(int i = 0;i<c;i++) { int from,to,val; scanf("%d%d%d",&from,&to,&val); add(from,to,-val); } if(spfa()) printf("YES"); else printf("NO"); printf(" "); } return 0; }