题目链接:http://poj.org/problem?id=3259
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 65132 | Accepted: 24260 |
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
Hint
For farm 1, FJ cannot travel back in time.
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this。
题意:判断给定的图是否存在负权图!(环图权值之和为负数即可)
思路:bellman-ford 或者 spfa判断负权环是否存在!
(1)spfa算法:
#include<math.h>
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
const int maxn = 1e3+10;
const int inf = 0x3f3f3f3f;
int vis[maxn],num[maxn];
int dis[maxn];
int n,m,w,u,v,w1,s,e,t;
struct NODE{
int e;
int w;
int next;
}edge[700*700];
int head[maxn],cnt;
void add(int u,int v,int w) //存图
{
edge[cnt].e = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
}
int spfa() //spfa核心算法
{
memset(dis,inf,sizeof dis);
memset(vis,0,sizeof vis);
memset(num,0,sizeof num);
queue<int>q;
q.push(1);
dis[1] = 0;
vis[1] = 1;
num[1] = 1;
while(!q.empty())
{
int now = q.front();
q.pop();
vis[now] = 0;
for(int i=head[now];i!=0;i=edge[i].next) //找啊找,不多说
{
int u = edge[i].e;
if(dis[u] > dis[now] + edge[i].w)
{
dis[u] = dis[now] + edge[i].w;
if(!vis[u])
{
vis[u] = 1;
q.push(u);
num[u]++;
if(num[u]>=n) //如果某个点入队大于等于n次,那么说明存在负权图
return 1;
}
}
}
}
return 0;
}
int main()
{
int t1;
scanf("%d",&t1);
while(t1--)
{
cnt = 1;
memset(head,0,sizeof head);
scanf("%d %d %d",&n,&m,&w);
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&u,&v,&w1); //这步加双向边
add(u,v,w1);
add(v,u,w1);
}
for(int i=1;i<=w;i++)
{
scanf("%d %d %d",&s,&e,&t); //这步加单向边
add(s,e,-1*t);
}
int ans = spfa();
if(ans==0)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}
(2)bellman-ford算法(算法书上的,挺好的)
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
struct Edge{
int u;
int v;
int w;
}e[550*550];
int d[505];
int m,n;
int bellman_ford()
{
int i,j,k,x,y,flag;
for(i=1;i<n;i++)
d[i] = inf;
d[0] = 0;
for(k=1;k<n;k++)
{
flag = 0;
for(i=0;i<m;i++)
{
x = e[i].u;
y = e[i].v;
if(d[x] < inf && d[y] > d[x] + e[i].w)
{
d[y] = d[x] + e[i].w;
flag = 1;
}
}
if(flag == 0) //如果经过某一次变换之后的flag不再改变,则后面一定不再会发生变化
break;
}
for(i=0;i<m;i++)
{
x = e[i].u;
y = e[i].v;
if(d[y] > d[x] + e[i].w)
return 0;
}
return 1;
}
int main()
{
int F,M,W,S,E,T,i;
scanf("%d",&F);
while(F--)
{
m = 0;
scanf("%d %d %d",&n,&M,&W);
for(i=0;i<M;i++)
{
scanf("%d %d %d",&S,&E,&T);
e[m].u = S;
e[m].v = E;
e[m++].w = T;
e[m].u = E;
e[m].v = S;
e[m++].w = T;
}
for(i=0;i<W;i++)
{
scanf("%d %d %d",&S,&E,&T);
e[m].u = S;
e[m].v = E;
e[m++].w = -T;
}
if(bellman_ford())
printf("NO\n");
else
printf("YES\n");
}
return 0;
}