Insect holes

Insect holes

 

Problem Description

When the farmer John in the reclaimed his farm, found many strange insect holes. These insects hole is unidirectional, and may be from the inlet to the outlet, and that the time period backwards. Each farm John comprising N (1≤N≤500) block, a number from 1 ~ N, there are M N between the fast (1≤M≤2500) road, W (1≤W≤200) a insect holes.
Because John is an avid fan of time travel, he wanted to try to do this one thing: From a piece of land, by some paths and insect holes, return to the starting point, and the time earlier than the departure time, so perhaps he could meet him themselves. John tried to help determine feasibility. He will provide his full map F (1≤F≤5) farms. Each time needed to finish the road no more than 10 000 seconds, each insect holes regressive event not more than 10 000 seconds.

Input

Input document acts integer F, F represents a test data, the test data for each depict a farm. Next is the data F farms.
Behavior of each of the first three farms integer: N, M and W. The second row to the M + 1 lines, each behavior three integers: S, T and E, represents a two-way path from S to E, the desired event through this path T seconds, at most between the two one way. Of M + 2 ~ M + W + 1 of rows of three integers: S, T and E, represents a unidirectional path from S to E, and back through the road such that the time T seconds.

Output

For each test data, if John can achieve their goals, output "YES"; otherwise, the output "NO".

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

Explanation:

The subject is easy to see that a judge has no negative ring topic, I use the SPFA algorithm , Baidu, and then learn about the algorithm, this problem is equivalent to the template title. Then the test data, the last set of test cases does not wrap. Zhendi is gay.

  1 #include<bits/stdc++.h>
  2 
  3 using namespace std;
  4 const int N = 5210;
  5 const int INF = 0xFFFFFFF;
  6 
  7 struct edge{
  8   int v, w, next;
  9 }edges[N];
 10 
 11 int dist[N], num[N], frist[N];
 12 bool vis[N];
 13 int n, m, w;
 14 int e_sums; 
 15 
 16 int Scan() { //输入挂
 17   int res = 0, ch, flag = 0;
 18   if ((ch = getchar()) == '-')
 19     flag = 1;
 20   else if (ch >= '0' && ch <= '9')
 21     res = ch - '0';
 22   while ((ch = getchar()) >= '0' && ch <= '9')
 23     res = res * 10 + ch - '0';
 24   return flag ? -res : res;
 25 }
 26 
 27 void Out (int a) {    //输出挂
 28   if (a > 9)
 29     Out(a / 10);
 30   putchar(a % 10 + '0');
 31 }
 32 
 33 void addEdge (int u, int v, int w) {
 34   edges[e_sums].v = v;
 35   edges[e_sums].w = w;
 36   edges[e_sums].next = frist[u];
 37   frist[u] = e_sums++;
 38 }
 39 
 40 bool spfa() {
 41   queue<int> que;
 42   dist[1] = 0;
 43   vis[1] = true;
 44   que.push(1);
 45   num[1]++;
 46   while (que.size()) {
 47     int u = que.front();
 48     que.pop();
 49     vis[u] = false;
 50     for (int i = frist[u]; i != -1; i = edges[i].next) {
 51       int v = edges[i].v;
 52       if (dist[v] > dist[u] + edges[i].w) {
 53         dist[v] = dist[u] + edges[i].w;
 54         if ( !vis[v] ) {
 55           vis[v] = true;
 56           que.push(v);
 57           num[v]++;
 58           if (num[v] > n) {
 59             return true;
 60           }
 61         }
 62       }
 63     }
 64   }
 65   return false;
 66 }
 67 
 68 
 69 int main() {
 70   int t;
 71   //scanf("%d", &t);
 72   t = Scan();
 73   int gz = 1;
 74   while (t--) {
 75     if (!gz)putchar('\n');
 76     gz = 0;
 77     e_sums = 0;
 78     memset(frist, -1, sizeof(frist));
 79     memset(vis, false, sizeof(vis));
 80     memset(num, 0, sizeof(num));
 81 
 82     //scanf("%d %d %d", &n, &m, &w);
 83     n = Scan(), m = Scan(), w = Scan();
 84     for (int i = 1; i <= n; i++) {
 85       dist[i] = INF;
 86     }
 87    
 88 
 89     int x, y, z;
 90     for (int i = 0; i < m; i++) {
 91       //scanf("%d %d %d", &x, &y, &z);
 92       x = Scan(), y = Scan(), z = Scan();
 93       addEdge(x, y, z);
 94       addEdge(y, x, z);
 95     }
 96     
 97     for (int i = 0; i < w; i++) {
 98       //scanf("%d %d %d", &x, &y, &z);
 99       x = Scan(), y = Scan(), z = Scan();
100       addEdge(x, y, -z);
101     }
102 
103     if ( spfa() ) {
104       printf("YES");
105     } else {
106       printf("NO");
107     }
108   }
109   return 0;
110 }
View Code

I feel the students, forced to try out this format the wrong solution, this is really big brother.

Guess you like

Origin www.cnblogs.com/gznb/p/11208116.html