POJ 3259 Wormholes Bellman题解

本题就是需要检查有没有负环存在于路径中,使用Bellman Ford算法可以检查是否有负环存在。

算法很简单,就是在Bellman Ford后面增加一个循环判断就可以了。

题目故事很奇怪,小心读题。

#include <stdio.h>
#include <string.h>
#include <limits.h>

const int MAX_N = 501;
const int MAX_M = 2501;
const int MAX_W = 201;

struct Edge
{
	int src, des, wei;
	//Edge(int s, int d, int w) : src(s), des(d), wei(w) {}
};

Edge edge[(MAX_M<<1)+MAX_W];
int dist[MAX_N];

int N, M, W, F;

bool cycleBellmanFord()
{
	for (int i = 1; i <= N; i++) dist[i] = INT_MAX;
	dist[1] = 0;
	for (int i = 1; i < N; i++)
	{
		bool seperate = true;
		for (int j = 0; j < (M<<1)+W; j++)
		{
			if (dist[edge[j].src] != INT_MAX &&
				dist[edge[j].src]+edge[j].wei < dist[edge[j].des])
			{
				dist[edge[j].des] = dist[edge[j].src]+edge[j].wei;
				seperate = false;
			}
		}
		if (seperate) break;
	}
	for (int j = 0; j < (M<<1)+W; j++)
	{
		if ( dist[edge[j].src] != INT_MAX &&
			dist[edge[j].src]+edge[j].wei < dist[edge[j].des]) return true;
	}
	return false;
}

int main()
{
	scanf("%d", &F);
	while (F--)
	{
		scanf("%d %d %d", &N, &M, &W);
		int i = 0;
		for ( ; i < (M<<1); i++)
		{
			scanf("%d %d %d", &edge[i].src, &edge[i].des, &edge[i].wei);
			i++;
			edge[i].des = edge[i-1].src;
			edge[i].src = edge[i-1].des;
			edge[i].wei = edge[i-1].wei;
		}
		for ( ; i < (M<<1)+W; i++)
		{
			scanf("%d %d %d", &edge[i].src, &edge[i].des, &edge[i].wei);
			edge[i].wei = -edge[i].wei;
		}
		if (cycleBellmanFord()) puts("YES");
		else puts("NO");
	}
	return 0;
}

POJ 3259 Wormholes Bellman题解

时间: 2024-08-12 16:26:28

POJ 3259 Wormholes Bellman题解的相关文章

POJ 3259 Wormholes SPFA算法题解

本题其实也可以使用SPFA算法来求解的,不过就一个关键点,就是当某个顶点入列的次数超过所有顶点的总数的时候,就可以判断是有负环出现了. SPFA原来也是可以处理负环的. 不过SPFA这种处理负环的方法自然比一般的Bellman Ford算法要慢点了. #include <stdio.h> #include <string.h> #include <limits.h> const int MAX_N = 501; const int MAX_M = 2501; const

POJ 3259 Wormholes (图论---最短路 Bellman-Ford || SPFA)

链接:http://poj.org/problem?id=3259 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 BE

ACM: POJ 3259 Wormholes - SPFA负环判定

POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu 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 pa

[2016-04-03][POJ][3259][Wormholes]

时间:2016-04-03 20:22:04 星期日 题目编号:[2016-04-03][POJ][3259][Wormholes] 题目大意:某农场有n个节点,m条边(双向)和w个虫洞(单向),(走虫洞可以回到过去的时间),问能否和过去的自己相遇 分析: 题目意思就是给定一个带负权的图,问是否存在负圈, spfa_bfs spfa_dfs bellman_ford #include <vector> #include <queue> #include <algorithm&

POJ 3259 Wormholes(SPFA)

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! Eac

[ACM] POJ 3259 Wormholes (bellman-ford最短路径,判断是否存在负权回路)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29971   Accepted: 10844 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 p

Poj 3259 Wormholes 负环判断 SPFA &amp; BellmanFord

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <

poj 3259 Wormholes(Bellman-Ford)

poj 3259 Wormholes 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 entere

poj 3259 Wormholes (Bellman-ford)

链接: poj 3259 题意:一个famer有一些农场,这些农场里面有一些田地,田地里面有一些虫洞,田地和田地之间有路(双向的),即从a到b和从b到a时间都为c.虫洞的性质:时间倒流.即通过虫洞从a到b所花时间为 -c(单向的).问从某块田出发,他能否通过虫洞的性质回到出发点前 思路:这题实际就是判断是否存在负权回路,可以用SPFA算法或Bellman-Ford算法判断.若存在负权回路,则可以达到目的,否则不可以. Bellman-ford算法 #include<stdio.h> #incl