POJ_3259(Wormholes)(SPFA判断负权回路)
Wormholes
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 76 Accepted Submission(s) : 31
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 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.
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 toE that also moves the traveler backT seconds.
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
NO YES
思路:该题就是判断是否有负权回路,若有的话,(最终花费的总时间为负数)则该人能回到(刚从起点出发之前)的某个时刻,看到以前的自己。
原理:如果存在负权值回路,那么从源点到某个顶点的距离就可以无限缩短,因此就会无限入队,所以在SPFA中统计每个顶点的入队次数,如果超过了n个(顶点个数)则说明存在负权值回路。
注意:该题任意选一个起点都可以(起点不同,但不会影响负权回路的存在(找到负权回路,一定可以把相关点设为起点,就可以完成题目要求,从起点回到起点),所以只要证明存在负权回路即可),只要证明存在负权回路。刚开始没有弄清楚,所以设置了多个起点(对多个起点进行判断),结果运行时间比较长,最后看了别人的解题报告,才发现设置一个起点就可以了。
My solution:
/*2015.8.21*/
判断多个起点:
耗时:1750MS
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
int head[550],mark[550],d[550],cnt[550];
int k,N;
struct stu
{
int from,to,val,next;
}map[500000];
void edage(int a,int b,int c)
{
stu E;
k++;
E.from=a;
E.to=b;
E.val=c;
E.next=head[a];
head[a]=k;
map[k]=E;
}
void SPFA(int s)
{
int i, v,u;
d[s]=0;
queue<int>q;
mark[s]=1;
q.push(s);
cnt[s]++;
while(!q.empty())
{
u=q.front();
q.pop();
mark[u]=0;
for(i=head[u];i!=-1;i=map[i].next)
{
v=map[i].to;
if(d[v]>d[u]+map[i].val)
{
d[v]=d[u]+map[i].val;
if(!mark[v])
{
mark[v]=1;
q.push(v);
cnt[v]++;
if(cnt[v]>N)
return ;
}
}
}
}
}
int main()
{
int a,b,c,i,s,e,t,n,M,W;
scanf("%d",&n);
while(n--)
{
k=-1;
memset(head,-1,sizeof(head));
scanf("%d%d%d",&N,&M,&W);
for(i=0;i<M;i++)
{
scanf("%d%d%d",&s,&e,&t);
edage(s,e,t);
edage(e,s,t);
}
for(i=0;i<W;i++)
{
scanf("%d%d%d",&s,&e,&t);
edage(s,e,-t);
}
for(i=1;i<=N;i++)
{
memset(mark,0,sizeof(mark));
memset(d,0x3f,sizeof(d));
memset(cnt,0,sizeof(cnt));
SPFA(i);/*i为起点,且的d[i]=0*/
if(d[i]<0)/*判断更新后的d[i]是否小于0,若小于0,说明存在负权回路。*/
{
printf("YES\n");
break;
}
}
if(i==N+1)
printf("NO\n");
}
return 0;
}
只判断1个起点:
耗时:188MS
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
int head[550],mark[550],d[550],cnt[550];
int k,N;
struct stu
{
int from,to,val,next;
}map[500000];
void edage(int a,int b,int c)
{
stu E;
k++;
E.from=a;
E.to=b;
E.val=c;
E.next=head[a];
head[a]=k;
map[k]=E;
}
void SPFA(int s)
{
int i, v,u;
d[s]=0;
queue<int>q;
mark[s]=1;
q.push(s);
cnt[s]++;
while(!q.empty())
{
u=q.front();
q.pop();
mark[u]=0;
for(i=head[u];i!=-1;i=map[i].next)
{
v=map[i].to;
if(d[v]>d[u]+map[i].val)
{
d[v]=d[u]+map[i].val;
if(!mark[v])
{
mark[v]=1;
q.push(v);
cnt[v]++;
if(cnt[v]>N)/*当一个源点入队次数大于节点数时,说明是负权回路*/
{
printf("YES\n");
return ;
}
}
}
}
}
printf("NO\n");
}
int main()
{
int a,b,c,i,s,e,t,n,M,W;
scanf("%d",&n);
while(n--)
{
k=-1;t=0;
memset(head,-1,sizeof(head));
memset(mark,0,sizeof(mark));
memset(d,0x3f,sizeof(d));
memset(cnt,0,sizeof(cnt));
scanf("%d%d%d",&N,&M,&W);
for(i=0;i<M;i++)
{
scanf("%d%d%d",&s,&e,&t);
edage(s,e,t);/*无向边*/
edage(e,s,t);
}
for(i=0;i<W;i++)
{
scanf("%d%d%d",&s,&e,&t);/*注意虫洞是有向边,且权值为负*/
edage(s,e,-t);
}
SPFA(1);
}
return 0;
}
本人菜鸟,对于判断负权回路中是入队次数>N(N为节点数)还是入队次数>=N,至今仍没有弄懂应该是哪个。
在判断负权回路时,在网上看了好多人的博客写的都不一样,有的是判断源点入队次数大于等于节点数,存在负权回路。而有的博客写的是入队次数大于节点数时 ,存在负权回路。
想了好久之后,我感觉第二种方法更好。因为既然说明存在负权回路,多判断一次,肯定不会影响它是否是负权回路(负权回路就是无限循环,多循环一次不影响它是负权回路)。
倘若恰好入队次数等于节点数,但不是复权回路,第一种方法就出错了。所以以后还是用第二种更好一点。
以上只是本人拙见。