poj1860 Bellman_ford算法

//poj1860 图论  只要有一个环,实现增加钱的目的,无限次循环这个环然后退出即可。
//Bellman_ford无限松弛者就是正环。参考别人代码思路 
#include <iostream>
#include <cmath>
using namespace std; 
double dist[110];
int num;
struct edge{
	int st, ed;
	double cost, w;
}e[220];
void addEdge(int x, int y, double r1, double c1)
{
	e[num].st = x;
	e[num].ed = y;
	e[num].cost= c1;
	e[num++].w = r1;
}
bool Bellman(int n, int m, int s, double v)
{
	bool flag = false;
	for(int i = 0; i <= n; i++)
		dist[i] = 0;
	dist[s] = v;
	for(int i = 1; i < n; i++)
	{
		for(int j = 0; j < num; j++)
		{
			int v= e[j].st;
			int u = e[j].ed;
			if(dist[u] < (dist[v] - e[j].cost) * e[j].w)
			{
				dist[u] = (dist[v] - e[j].cost) * e[j].w;
				flag = true;
			}
		}
		if(flag == false)
			break;
	}
	for(int i = 0; i < num; i++)
		if(dist[e[i].ed] < (dist[e[i].st] - e[i].cost) * e[i].w)
			return true;
	return false;
}
int main()
{
	int n, m, s;
	double v;
	cin>>n>>m>>s>>v;
	while(!cin.eof())
	{
		num = 0;
		while(m--)
		{
			int id1, id2;
			double r1, c1, r2, c2;
			cin>>id1>>id2>>r1>>c1>>r2>>c2;
			addEdge(id1, id2, r1, c1);
			addEdge(id2, id1, r2, c2);
		}
		bool ok = Bellman(n, m, s, v);
		if(ok)
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
		cin>>n>>m>>s>>v;
	}
	return 0;	
}
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/chchlh/article/details/41486751
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞