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: A single integer, F. F farm descriptions follow.
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
Lines 1…F: For each farm, output “YES” if FJ can achieve his goal, otherwise output “NO” (do not include the quotes).
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
思路:题目意思先给出三个数n,m,w,接下来给出m条无向图路线,然后再接着给出w条有向的路线(虫洞),问能不能从一个点出发绕绕绕又经过虫洞回到出发点,那么虫洞路线的权值就相当于负值,存在负权回路的话才能绕回起点。
所以题目意思就是:
1、先给出m条无向的正权路线,再给出w条有向的负权路线。
2、判断这个图是否存在负权回路。
3、可以用Bellman-ford算法判断是否存在负权回路,spfa算法是对前一个算法的优化。
spfa算法:
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;
const int N = 510, M = 5210;
int n,m,k;
int h[N],w[M],e[M],ne[M],idx;
int dist[N],cnt[N],q[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(cnt,0,sizeof cnt);
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;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&k);
memset(h,-1,sizeof h);
idx = 0;
for(int i = 0; i < m; 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 < k; 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;
}
Bellman-ford算法:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 1e5;
struct Edge{
int a,b,c;
}edges[N];
int n,m,k;
int dist[N],backup[N];
bool Bellman_ford(){
for(int i = 0; i < n; i++){
memcpy(backup,dist,sizeof dist);
for(int j = 0; j < m; j++){
Edge e = edges[j];
if(dist[e.b] > backup[e.a] + e.c){
dist[e.b] = backup[e.a] + e.c;
if(i == n - 1) return true;
}
}
}
return false;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&k);
int a,b,c,cnt = 0;
for(int i = 0; i < m; i++){
scanf("%d%d%d",&a,&b,&c);
edges[cnt++] = {a,b,c};
edges[cnt++] = {b,a,c};
}
for(int i = 0; i < k; i++){
scanf("%d%d%d",&a,&b,&c);
edges[cnt++] = {a,b,-c};
}
m = cnt;
if(Bellman_ford()) puts("YES");
else puts("NO");
}
return 0;
}