POJ-1860(Bellman()重要应用)
来源:互联网 发布:盛科网络上市没 编辑:程序博客网 时间:2024/06/02 01:48
bellman()算法的有点就是可以判断是否又环,,,而正如这道题目一样,
我开始的算法就是求是不是经过一次到达起点的时候比原来起点的钱要多,如果多就说明可以挣钱,但其实这个思路还是有瑕疵的.,
因为有时候后面的可以增长钱,但是前面的却不能增长反而减少,而这个时候其实是可以挣钱的,你想他就是换一次就挣一分钱.而你能防住他一直在那个银行换么...
换个几万次 人家1毛钱也能换成好几千了 而这个时候在换回来自己原来的货币 不还是赚钱,,,
所以这个题目的重点就是求是不是有增长环,,而这个时候用bellman就正确了
贴出代码:
#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>int N,M,S;//N represents the number of points,and M stands for the number of edge,double V;struct Edge{int a;int b;double rate;double sm;}e[205];double dis[205];int relax(int a,int b,double c,double d){if(dis[b]<(dis[a]-d)*c){dis[b]=(dis[a]-d)*c;//printf("%d____%lf\n",b,dis[b]);return 1;}else return 0;}int Bellman(){for(int i=1;i<=N;i++)dis[i]=-1.0;dis[S]=V;for(int j=1;j<N;j++){int temp=0;for(int i=1;i<=M*2;i++){if(relax(e[i].a,e[i].b,e[i].rate,e[i].sm))temp=1;}if(!temp)break;}//printf("%lf\n",dis[S]);for(int i=1;i<=M*2;i++){if(relax(e[i].a,e[i].b,e[i].rate,e[i].sm))return 1;}return 0;}int main(){while(scanf("%d%d%d%lf",&N,&M,&S,&V)!=EOF){int a,b;double c,d,ee,f;for(int i=1;i<=M;i++){scanf("%d%d%lf%lf%lf%lf",&a,&b,&c,&d,&ee,&f);e[i].a=a,e[i].b=b;e[i].rate=c,e[i].sm=d;e[i+M].a=b,e[i+M].b=a;e[i+M].rate=ee,e[i+M].sm=f;}int ans=Bellman();if(ans==1)printf("YES\n");elseprintf("NO\n");}return 0;}
- POJ-1860(Bellman()重要应用)
- poj 1860 bellman-ford
- poj 1860 bellman 求正环
- poj 1860 Bellman-Ford
- poj 1860 (bellman-ford)
- poj 1860 Bellman算法
- poj 1860 Bellman-Ford
- poj 1860 bellman ford算法
- POJ 1860 Currency Exchange Bellman
- poj 1860 Currency Exchange (bellman)
- poj-1860-Currency Exchange-Bellman
- poj 1860 Currency Exchange bellman
- POJ-1860-Currency Exchange(bellman)
- POJ 1860 Currency Exchange Bellman-Ford
- POJ 1860 bellman-ford 的变形
- POJ-1860-Currency Exchange-Bellman-ford
- poj 1860 Currency Exchange :bellman-ford
- POJ 1860 Currency Exchange(Bellman-Ford)
- 如何在JNI编程中使用logCat
- 懒汉模式与饿汉模式
- 删除.svn文件(windows和linux)
- get row data value& datatable filter & new datatable from datatable
- 不同语言写出来的pe文件不相同
- POJ-1860(Bellman()重要应用)
- 动态规划 - 最长公共子序列
- 【解题报告】 POJ 1611 The Suspects 并查集基础 (一点自己的感悟)
- 多分方法的再讨论
- POJ 2528 – Mayor's posters
- getDefinitionByName巧妙应用
- JSONHelper
- 一个int 数组,里面数据无任何限制,要求求出所有这样的数a[i],其左边的数都小于等于它,右边的数都大于等于它。能否只用一个额外数组和少量其它空间实现。
- 插入符号的函数和TYPER程序