Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21769 | Accepted: 7748 |
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 toF (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
题意:John的农场里field块地,path条路连接两块地,hole个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己。 自己做了两天 改了两天 多谢姬兴成大力帮忙改题~ 第一次用spfa算法 还不熟~
代码:
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> using namespace std; int map[506][506]; bool vis[506]; int n,m,hold; int dir[506]; int num[506]; const int maxn= 999999; int spfa(int s) { queue<int> que; while (!que.empty()) que.pop(); for (int i=1;i<=n;i++) { dir[i]=maxn; } memset(vis, 0, sizeof(vis)); memset(num, 0, sizeof(num)); que.push(s); dir[s]=0; num[s]=1; vis[s]=1; while (!que.empty()) { int u=que.front(); que.pop(); vis[u]=1; //要是wa的话 要注意这个地方 为什么为0 不是1 for (int i=1;i<=n;i++) { if (map[u][i]<maxn&&dir[u]<maxn&&dir[i]>dir[u]+map[u][i]) { dir[i]=dir[u]+map[u][i]; cnt[i]++;//是每个点入队的次数 不是总共的入队次数~ 刚开始直接tail++ ;tail>=n;计算的 结果一值没有过 if (cnt[i] >=n)//注意这个if 应该放在外面 { return 1; } if (!vis[i]) { vis[i]=1; que.push(i); } } } vis[u]=0; } return 0; } int main() { int cas; cin>>cas; int a,b,w; while (cas--) { cin>>n>>m>>hold; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) { map[i][j]=maxn; } for (int i=1;i<=m;i++) { cin>>a>>b>>w; if(w<map[a][b])//可能存在重边 所以一定要进行判断 这样当有相同的a到b的时候 会选比较小的那个~ map[a][b]=map[b][a]=w; } for (int i=1;i<=hold;i++) { cin>>a>>b>>w; map[a][b]=-w;//就是这个地方 可以过去 但是不能回去~ 我擦~ } if (spfa(1)) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }