# PKU ACM 1860 Bellman - Ford 算法

``````#include <iostream>
#include <fstream>
using namespace std;

struct Edge
{
int a,b;
double r,c;
};
double maxDistance[300];
bool bellman(int s,int n,int m,Edge *e,double v)
{
int i,j;
for( i = 0; i <= n; i++)
{
maxDistance[i] = -100000000;
}
maxDistance[s] = v;
for( i = 0; i < n; i++)
{
bool stop = true;
for( j = 0 ; j < m ; j++)
{
//从a到b b是目标点
if(maxDistance[e[j].a] > 0
&& maxDistance[e[j].b] < (maxDistance[e[j].a] - e[j].c)*e[j].r)
{
maxDistance[e[j].b] = (maxDistance[e[j].a] - e[j].c)*e[j].r;
stop = false;
}
}
if(stop)
{
return false;
}
}
return true;
}
int main()
{
int n,m,s;
int i,f,t;
double fc,tc,fr,tr,v;
Edge e[300];
ifstream input;
input.open("data.txt",std::ios::in);
while(input>>n>>m>>s>>v)
{
for( i = 0; i < m; i++)
{
input>>f>>t>>fr>>fc>>tr>>tc;
Edge e1 = {f,t,fr,fc};
Edge e2 = {t,f,tr,tc};
e[i] = e1;
e[i+m] = e2;
}
if(bellman(s,n,2*m,e,v))
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
}``````
原文作者：Bellman - ford算法
原文地址: https://blog.csdn.net/hopeztm/article/details/7021078
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。