# poj1860_bellman ford

还是货币兑换的题，想找是否货币经过无数次兑换后是否会升值。兑换边符合（兑换货币值-兑换费用）*兑换率。

典型的bellman ford。但是忘了。。。。

复习下bellman-ford

#include
#include
#define E 10001
#define N 101

typedef struct{
int s, e;
double w, c;
}EDGE;

int n,m,s;
double v, D[N];
EDGE edges[E];

main(){
int i, j, flag;

scanf(“%d %d %d %lf”, &n, &m, &s, &v);
for(i=1;i<=m;i++){
scanf(“%d %d %lf %lf %lf %lf”,&edges[2*i-1].s,&edges[2*i-1].e, &edges[2*i-1].w, &edges[2*i-1].c, &edges[2*i].w, &edges[2*i].c);
edges[2*i].s = edges[2*i-1].e;
edges[2*i].e = edges[2*i-1].s;
}
//memset(D,0,sizeof(D));
for(i=1; i<=n; i++)
D[i] = 0.0;
D[s] = v;

//bellman-ford
for(i=1;i<=n;i++){
flag = 0;
for(j=1;j<=2*m;j++){ //遍历边做松弛操作
double temp = (D[edges[j].s] – edges[j].c)*edges[j].w;
if(D[edges[j].e] < temp){
//if(D[edges[j].e] < (D[edges[j].s] – edges[j].c)*edges[j].w){
flag = 1;
// printf(“update %d. former = %lf, later = %lf\n”,edges[j].e, D[edges[j].e], (D[edges[j].s] – edges[j].c)*edges[j].w);
D[edges[j].e] = (D[edges[j].s] – edges[j].c)*edges[j].w;

}
}
if(0 == flag)
break;
}

if(1 == flag){
printf(“YES\n”);
}else{
printf(“NO\n”);
}

system(“pause”);
return 0;
}

原文作者：Bellman - ford算法
原文地址: https://blog.csdn.net/u010456139/article/details/8874792
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。