【最短路径入门专题1】K - Wormholes POJ3259 【SPFA】【Bellman-Ford】

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..NM (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, FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: NM, and W 
Lines 2.. M+1 of each farm: Three space-separated numbers ( SET) that describe, respectively: a bidirectional path between S and E that requiresT seconds to traverse. Two fields might be connected by more than one path. 
Lines M+2.. MW+1 of each farm: Three space-separated numbers ( SET) 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.

题意:输入t,再输入t组样例,每组样例输入n,m,w,每行输入三个整数,起点,终点,权值。m行数是边权为正,w行数边权为负,判断他是否能够回到1,或者说判断是否存在负权环.


思路:一种是用Bellman-Ford写,另一种是用队列优化过SPFA 写。先上用BellmanAC的代码。

#include<stdio.h> #include<string.h> #define N 600 int dis[600]; struct edge{ int from; int to; int w; }; edge e[5000]; int n,m,t,w; int spfa() { int i,j,flag; for(j = 1; j <= n; j ++) { flag = 0;//检测是否存在负权回路 for(i = 1; i <= m; i ++) { if(dis[e[i].from] > dis[e[i].to]+e[i].w) { dis[e[i].from] = dis[e[i].to] + e[i].w ; flag = 1; } if(dis[e[i].to] > dis[e[i].from] + e[i].w) { dis[e[i].to] = dis[e[i].from ]+e[i].w; flag = 1; } } for(;i <=w+m; i ++) { if(dis[e[i].to] > dis[e[i].from] - e[i].w) { dis[e[i].to ] = dis[e[i].from ]-e[i].w ; flag = 1; } } if(!flag) break; if(flag&&j == n)//最多只有n-1条边,如果枚举到n条边,说明存在负权边 return false; } return true; } int main() { int i; scanf("%d",&t); while(t --) { scanf("%d%d%d",&n,&m,&w); memset(dis,-1,sizeof(dis)); for(i = 1; i <= m+w; i ++)//存入边 scanf("%d%d%d",&e[i].from,&e[i].to,&e[i].w ); if(spfa())//判断是否有负权边 printf("NO\n"); else printf("YES\n"); } return 0; }

SPFA的AC代码

#include<stdio.h>
#include<iostream>
#include<queue> 
#include<string.h>
using namespace std;
#define N 600
int first[N],dis[N];
int n,m,w;
int num;
struct edge{
	int to,w,next;
};
edge e[50000];
void addedge(int a,int b,int c)
{
	e[num].to = b;
	e[num].w = c;
	e[num].next = first[a];
	first[a] = num++;
}

bool spfa(int s)
{
	int used[N];//用来记录一个顶点入队次数 
	bool book[N];//标记该点是否在队列中 
	int head,tail,i,to,now;
	queue<int>Q;
	memset(used,0,sizeof(used));
	memset(book,false,sizeof(book));
	Q.push(s); 
	book[s] = true;
	used[s]++;
	dis[s] = 0;
	while(!Q.empty())
	{
		now = Q.front() ;
		Q.pop() ;
		book[now] = false;
		for(i = first[now];i!=-1;i = e[i].next)
		{
			to = e[i].to ;
			if(dis[to]>dis[now]+e[i].w)
			{
				dis[to] = dis[now] + e[i].w ;
				used[to]++;
				if(used[to]>=n)//一个点使用超过n,一定存在负环 
					return false;
				if(!book[to])//如果顶点不在队列中,入队 
				{
					book[to] = true;//标记为已经入队 
					Q.push(to) ;
				}
			}
		}
	} 
	return true;
}
int main()
{
	int i,a,b,c,t;
	scanf("%d",&t);
	while(t --)
	{
		scanf("%d%d%d",&n,&m,&w);
		num = 1;
		memset(dis,-1,sizeof(dis));
		memset(first,-1,sizeof(first));
		for(i = 1; i <= m; i ++)
		{
			scanf("%d%d%d",&a,&b,&c);
			addedge(a,b,c);
			addedge(b,a,c);
		}
		for(i = 1; i <= w; i ++)
		{
			scanf("%d%d%d",&a,&b,&c);
			addedge(a,b,-c);
		}
		for(i = 1; i <= n; i ++)
		{
			if(dis[i] == -1&&!spfa(i))
			{
				i = n+2;
				break;
			}
		}
		if(i == n+1)
			printf("NO\n");
		else
			printf("YES\n");
	}
	return 0;
}



说好的入门题呢????为啥一上来我连dijsktra都还没怎么用就直接上涉及负权边的题。









    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/hello_sheep/article/details/76737014
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞