SPFA find negative ring --------- wormhole

Farmer John discovered many amazing wormholes while visiting his many farms.
The wormhole is very peculiar. It can be regarded as a one-way path, through which you can return to a certain moment in the past (relative to before you entered the wormhole).
Farmer John ’s farm contains N fields, M paths (two-way), and W wormholes.
Now Farmer John hopes to be able to start from a field in the farm, go back to the past through some paths and wormholes, and rush to his departure place before his departure time.
He hopes to see himself before departure.
Please judge whether John can do this.
Below we will provide you with the number of farms owned by John F and complete information about each farm.
It is known that the time taken to travel any path does not exceed 10,000 seconds, and the time taken by any wormhole to bring him back will not exceed 10000 seconds.
Input format The
first line contains the integer F, indicating that John has a total of F farms.
For each farm, the first row contains three integers N, M, W.
The next M lines, each line contains three integers S, E, T, indicating that there is a path between the fields S and E, the time it takes to pass this path is T.
Next, in row W, each row contains three integers S, E, and T, indicating that there is a wormhole that walks from field S to field E. After walking through this wormhole, you can return to between T seconds.
Output format
A total of F lines are output, with one result per line.
If John can return to the departure place before the departure time, "YES" is output, otherwise "NO" is output.
Data range
1≤F≤51≤F≤5

1≤N≤5001≤N≤500,

1≤M≤25001≤M≤2500,

1≤W≤2001≤W≤200,

1≤T≤100001≤T≤10000,

1≤S, E≤N1≤S, E≤N
Input sample:
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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 510, M = 5210;
int n, m1, m2;
int h[N], e[M], w[M], ne[M], idx;
int dist[N];
int q[N], cnt[N];
bool st[N];
void add(int a, int b, int c){
 e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
bool spfa(){
 memset(dist, 0, sizeof dist);
 memset(cnt, 0, sizeof cnt);
 memset(st, 0, sizeof st);
  int hh = 0, tt = 0;
 for (int i = 1; i <= n; i ++){
  q[tt ++] = i;
  st[i] = true;
 }
  while(hh != tt){
  int t = q[hh ++];
  if (hh == N)   hh = 0;
  st[t] = false;
   for (int i = h[t]; ~i; i = ne[i]){
   int j = e[i];
   if (dist[j] > dist[t] + w[i]){
    dist[j] = dist[t] + w[i];
    cnt[j] = cnt[t] + 1;
    if (cnt[j] >= n)    return true;
    if (!st[j]){
     q[tt ++] = j;
     if (tt == N)   tt = 0;
     st[j] = true;
    }
   }
  }
 }
  return false;
}
int main(){
 int T;
 cin >> T;
 while(T --){
  scanf("%d%d%d", &n, &m1, &m2);
  memset(h, -1, sizeof h);
  idx = 0;
    for (int i = 0; i < m1; i ++){
   int a, b, c;
   scanf("%d%d%d", &a, &b, &c);
   add(a, b, c), add(b, a, c);
  }
    for (int i = 0; i < m2; i ++){
   int a, b, c;
   scanf("%d%d%d", &a, &b, &c);
   add(a, b, -c);
  }
    if (spfa())    puts("YES");
  else           puts("NO");
 }
  return 0;
}
164 original articles published · Like 112 · Visitors 6771

Guess you like

Origin blog.csdn.net/qq_45772483/article/details/105469818