POJ3259 Wormholes 【Bellmanford判断是否存在负回路】

很简单的bellmanford题目,这里比较详细:http://blog.csdn.net/lyy289065406/article/details/6645790

直接代码

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int SIZE=11111;
const int MAXLEN=1<<30;
struct sss
{
	int s,e,v;
}ed[SIZE];
int N,M,WH;
int dis[SIZE];
int ednum;
bool Bellman()
{
	for(int i=0;i<N-1;i++)
	{
		bool flag=false;
		for(int j=0;j<ednum;j++)
		{
			if(dis[ed[j].e]>dis[ed[j].s]+ed[j].v)
			{
				flag=true;
				dis[ed[j].e]=dis[ed[j].s]+ed[j].v;
			}
		}
		if(!flag)
			break;
	}
	for(int j=0;j<ednum;j++)
	{
		if(dis[ed[j].e]>dis[ed[j].s]+ed[j].v)
		{
			return true;
		}
	}
	return false;
}
int main()
{
	#ifndef ONLINE_JUDGE
		freopen("G:/1.txt","r",stdin);
		freopen("G:/2.txt","w",stdout);
	#endif
	int f,u,v,w;
	cin>>f;
	while(f--)
	{
		ednum=0;
		cin>>N>>M>>WH;
		for(int i=0;i<SIZE;i++)
		{
			dis[i]=MAXLEN;
		}
		for(int i=0;i<M;i++)
		{
			cin>>u>>v>>w;
			ed[ednum].s=u;
			ed[ednum].e=v;
			ed[ednum++].v=v;
			ed[ednum].e=u;
			ed[ednum].s=v;
			ed[ednum++].v=w;
		}
		for(int i=0;i<WH;i++)
		{
			cin>>u>>v>>w;
			ed[ednum].s=u;
			ed[ednum].e=v;
			ed[ednum++].v=-w;
		}
		if(Bellman())
			printf("YES\n");
		else
			printf("NO\n");
	}
    return 0;
}

POJ3259 Wormholes 【Bellmanford判断是否存在负回路】

时间: 2024-10-30 12:20:25

POJ3259 Wormholes 【Bellmanford判断是否存在负回路】的相关文章

POJ3259——Wormholes(Bellman-Ford+SPFA)

Wormholes DescriptionWhile 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 worm

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 entered the wormhole! Eac

POJ3259 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

poj 3259 bellman-ford判断是否存在负权回路

// // main.cpp // poj3259 // // Created by Fangpin on 15/5/28. // Copyright (c) 2015年 FangPin. All rights reserved. // #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using name

[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(最短路,判断有没有负环回路)

F - Wormholes Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u SubmitStatus Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a on

poj 3259 Wormholes(bellman-ford判断负环)

题目链接:http://poj.org/problem?id=3259 题目就是问你能否回到原点而且时间还倒回去了.题目中有些路中有单向的虫洞能让时间回到过去 所以只要将虫洞这条边的权值赋为负然后再判断有没有负环就行了. #include <iostream> #include <cstring> using namespace std; const int inf = 10001; int f , n , m , w ,dis[1001] , counts; struct TnT

POJ 3259 Wormholes( bellmanFord判负环)

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

poj3259 Wormholes --- spfa判负环

又写了个bellman模板一直RE求解啊... #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x