Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 38376 | Accepted: 14130 |
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, 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 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
#define _CRT_SECURE_NO_WARNINGS /*spfa思想2.//首先将起点弹入队列,用used数组标记i节点是否在队列中, 3.//然后从队列中弹出节点,判断从这个弹出节点能到达的每个节点的距离是否小于已得到的距离, 4.//如果是则更新距离,然后将其弹入队列,修改used数组。 */ /*第一次提交WA 因为map[][]双向路径会有重复因此等都输入完毕后再更新到数组中去,怎么改都不对,找不到原因,先用bellman-ford试下*/ /*第n次用bellman-ford提交 AC*/ #include <stdio.h> #define MAXINT 1005 #define MAXINTWOREHOLE 1005 #define MAXPATH 10005 int map[MAXINT][MAXINT]; int wormhole[MAXINTWOREHOLE][MAXINTWOREHOLE]; int distance[MAXINT]; int dijsavist[MAXINT]; //有哪些顶点已经算过dijksta最短路径了,这样就不用重复计算了 //题意了解的不对啊,自己以为是找到黑洞2点间的路径最短路径,然后如果最短路径比黑洞小,那输出NO,看来不对啊 //自己一直理解的还是有点问题的,上面理解的黑洞时间,其实是如果从黑洞过去时间越长说明返回到之前的时间越早,那越可能遇到之前的自己 //还有一个自己用dijkstra函数不对的,因为这个求最短路径和黑洞比较,但是有可能黑洞之前也会有路径,我现在是一个黑洞一个黑洞的比较 //这样不对的 //看百度说到bellman-ford算法,允许有负权,但是不允许有负回路(就是值的和是负的)这个自己不会的 typedef struct node { int from; int to; int len; }nodes; int F = 0; int N = 0; int M = 0; int W = 0; int top = 0; //spfa是需要一个队列的 int que[MAXINT*MAXINT*10]; //假设最多有MAXINT个点 int disinqu[MAXINT]; //是否在队列里 int head[10000]; nodes quedis[10000]; //一共有这么多条路双向的两条路和黑洞的路 void init() { int i = 0; int j = 0; for (i = 0; i < MAXINT;i++) { for (j = 0; j < MAXINT; j++) { map[i][j] = MAXPATH; } dijsavist[i] = 0; disinqu[i] = 0; } for (i = 0; i < MAXINTWOREHOLE; i++) { for (j = 0; j < MAXINTWOREHOLE; j++) { wormhole[i][j] = 0; } } for (i = 0; i < 10000; i++) { head[i] = -1; quedis[i].to = 0; quedis[i].len = 0; quedis[i].from = 0; } for (i = 0; i <( MAXINT*MAXINT * 10); i++) { que[i] = 0; } top = 0; return; } int bellmanford() { int i = 0; int j = 0; int flag = 0; for (i = 1; i <= N;i++) { distance[i] = MAXPATH; //距离弄成最小值 } distance[1] = 0; for (i = 1; i <= N;i++) { flag = 0; for (j = 0; j < top;j++) { if (distance[quedis[j].to] > distance[quedis[j].from] + quedis[j].len) { distance[quedis[j].to] = distance[quedis[j].from] + quedis[j].len; flag = 1; //说明路径有更新 } } if (0 == flag) //说明没更新了,那可以退出了 { break; } } //判断是否有负数环 for (j = 0; j < top; j++) { if (distance[quedis[j].to] > distance[quedis[j].from] + quedis[j].len) { return 1; } } return 0; } int spfa(int start) { int i = 0; int front = 0; int back = 0; for (i = 1; i <= N;i++) { distance[i] = MAXPATH; disinqu[i] = 0; dijsavist[i] = 0; } distance[start] = 0; //这个是把1作为源点了啊,这个看起来感觉不太对吧,为什么一定是1,我觉得不一定耶 que[back++] = start; dijsavist[start] = 1; while (front < back) { int x = que[front]; disinqu[x] = 0; for (int u = head[x]; u != -1;u=quedis[u].from) { if (distance[quedis[u].to] >(distance[x] + quedis[u].len)) { distance[quedis[u].to] = distance[x] + quedis[u].len; //上面有刷新 if (0 == disinqu[quedis[u].to]) { disinqu[quedis[u].to] = 1;//重新入队 dijsavist[quedis[u].to] += 1; //比如n个点(1到n),如果源点是1,去更新其他点,比如2点,如果其他点都能更新它, //那它就入队n-1次了,这样按理来说已经松弛完毕了,但是如果继续入队,说明存在负权回路了。 if (dijsavist[quedis[u].to] > N-1) return 1;//存在负环,直接return que[back++] = quedis[u].to; } } } front++; } return 0; } //把输入的路径都存到这边来 void add(int from, int to, int length) { quedis[top].to = to; quedis[top].len = length; quedis[top].from = head[from]; head[from] = top++; return; } int main() { int i = 0; int j = 0; int S = 0; int E = 0; int T = 0; int flag = 0; freopen("input.txt","r",stdin); scanf("%d",&F); while (F>0) { scanf("%d %d %d", &N, &M, &W); init(); flag = 0; top = 0; for (i = 1; i <= M;i++) { scanf("%d %d %d", &S, &E, &T); //if (map[S][E] > T) //{ map[S][E] = T; map[E][S] = T;//这个是two way //add(S, E, T); quedis[top].from = S; quedis[top].to = E; quedis[top].len = T; top += 1; quedis[top].from = E; quedis[top].to = S; quedis[top].len = T; top += 1; //} } for (i = 1; i <= W; i++) { scanf("%d %d %d", &S, &E, &T); //if (wormhole[S][E] > -T) //{ wormhole[S][E] = -T;//这个是one way 这个就是负路径 quedis[top].from = S; quedis[top].to = E; quedis[top].len = -T; top += 1; //add(S, E, -T); //} } //用spfa算法处理有负权的路径 //如果存在负环就返回,说明存在环路黑洞的时间长能够回到以前的时间 #if 0 for (i = 1; i <= N;i++) { if (1 == spfa(i)) //起点要一个个试 { printf("YES\n"); flag = 1; break; } } if (0 ==flag) { printf("NO\n"); } #endif /*bellman ford AC*/ if (1 == bellmanford()) { printf("YES\n"); } else { printf("NO\n"); } F -= 1; } return 0; }