POJ 3259 Wormholes(bellmanFord判断负环)

题目大意:
对于几条给定的道路,通过虫洞可以逆流时光,问是否存在某个点使得从这个点出发,再回到这个点时,所处的时间早于从该点出发的时间。
思路:

既然是多个点,我们就自然而然的想到了利用Floyd求多源最短路。。。然鹅他T掉了。。阿西吧,偷偷改了一下判断条件去掉了min改成了if评测姬抖了抖竟然过了hhhh (^ _^)

实际上只需要判断是否存在负环就行啦。

#include<iostream>
#include<cstdio>
#include<cstring>
#define INF 999999
using namespace std;
int F,N,M,W;
int s,e,t;
int cnt;
int dis[520];
struct Edge
{
	int u,v;
	int cost;
	// u go to v costs 'cost'
}edge[5040]={0};
void addedge(int ss,int ee,int tt)
{
	edge[cnt].u=ss;
	edge[cnt].v=ee;
	edge[cnt].cost=tt;
	cnt++;//count of edges
}

int Bellman()
{
	for(int i=1;i<=N;i++)
		dis[i]=INF;
	dis[1]=0;
	for(int i=1;i<N;i++)
		for(int j=0;j<cnt;j++)
			dis[edge[j].v]=min(dis[edge[j].v],(dis[edge[j].u]+edge[j].cost));
			
	for(int j=0;j<cnt;j++)
		if(dis[edge[j].v]>dis[edge[j].u]+edge[j].cost)
			return 0;		
	return 1;	
}
int main()
{
	cin>>F;
	
	for(int k=1;k<=F;k++) 
	{
		cin>>N>>M>>W;
		cnt=0;
				
		for(int i=1;i<=M;i++)
			 {
			 	cin>>s>>e>>t;
			 	addedge(s,e,t);
			 	addedge(e,s,t);
			 }
		for(int i=1;i<=W;i++) 
			{
				cin>>s>>e>>t;
				addedge(s,e,-t);
			}
		if(Bellman()) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	}

 return 0;
}
// 
//Buddha blesses the code with no bugs
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//

最后贴一下俺的

#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<cstdio>
#include<cmath>
#include<iomanip>
#include<sstream>
#include<queue>

#define pi 3.1415926535
#define me(a,b,c) memset(a,b,sizeof c)

#define eps 0.00000001
//#define x first
//#define y second

using namespace std;

typedef long long ll;
const int mod = 1e9 + 7;
typedef pair<int, int> pii;
const int N = 2020;

int n, m, k;
int t;
int g[N][N];

void init() {
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if (i == j)g[i][j] = 0;
			else g[i][j] = 0x3f3f3f3f;
}
int main() {
	scanf("%d", &t);
	while (t--)
	{
		int flag = 0;
		scanf("%d %d %d", &n, &m, &k);
		init();
		for (int i = 1; i <= m; i++) {
			int a, b, c;
			scanf("%d %d %d", &a, &b, &c);
			g[a][b] = g[b][a] = min(g[a][b], c);
		}
		for (int i = 1; i <= k; i++) {
			int a, b, c;
			scanf("%d %d %d", &a, &b, &c);
			g[a][b] = -c;
		}
		for (int z = 1; z <= n; z++)
		{
			for (int i = 1; i <= n; i++)
			{
				for (int j = 1; j <= n; j++) {
					if(g[i][j] > g[i][z] + g[z][j])
					g[i][j] = g[i][z] + g[z][j];
				}
				if (g[i][i] < 0) {
					flag = 1;
					break;
				}
			}
			if (flag)break;
		}
		
		if (flag)printf("YES\n");
		else printf("NO\n");
	}
}
// 
//Buddha blesses the code with no bugs
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值