poj1860--Currency Exchange
Bellman-ford算法的反向应用--正循环检查
/** \brief poj 1860 Bellman-Ford
*
* \param date 2014/7/24
* \param state AC
* \return memory 708K time 141ms
*
*/ #include <iostream>
#include <fstream>
#include <cstring> using namespace std; struct RateAndCom
{
//public:
int a;
int b;
double rate;
double Com;
};//Map[MAXN]; const int MAXN=101;
RateAndCom Map[101*2];
double dis[MAXN]; int N;//货币种数
int M;//兑换点数量
int S;//持有第s种货币
double V;//第s种货币本金
int allEdge; bool Bellman_Ford()
{
memset(dis,0,sizeof(dis));
dis[S]=V;
/*relax*/
bool flag;
for(int i=1;i<=N-1;i++)
{
flag=false;
for(int j=0;j<allEdge;j++)
if(dis[Map[j].b] < (dis[Map[j].a]-Map[j].Com)*Map[j].rate)
{
dis[Map[j].b] = (dis[Map[j].a]-Map[j].Com)*Map[j].rate;
flag=true;
}
if(!flag)
break;
} for(int k=0;k<allEdge;k++)
if(dis[Map[k].b] < (dis[Map[k].a]-Map[k].Com)*Map[k].rate)
return true; return false;
} int main()
{
//cout << "Hello world!" << endl;
//freopen("input.txt","r",stdin);
//while(scanf("%d %d %d %f",&N,&M,&S,&V)!=EOF)
while(cin>>N>>M>>S>>V)
{
allEdge=0;
for(int i=0;i<M;i++)
{
int a,b;
double Rab;
double Cab;
double Rba;
double Cba;
//cin>>a>>b>>Map[a][b].rate>>Map[a][b].Commission
//>>Map[b][a].rate>>Map[b][a].Commission;
cin>>a>>b>>Rab>>Cab>>Rba>>Cba;
Map[allEdge].a=a;
Map[allEdge].b=b;
Map[allEdge].rate=Rab;
Map[allEdge].Com=Cab;
allEdge++;
Map[allEdge].a=b;
Map[allEdge].b=a;
Map[allEdge].rate=Rba;
Map[allEdge].Com=Cba;
allEdge++;
}
//Bellman-Ford
if(Bellman_Ford())
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
转载请注明出处:http://blog.csdn.net/greenapple_shan/article/details/38307879
poj1860--Currency Exchange的更多相关文章
- POJ1860——Currency Exchange(BellmanFord算法求最短路)
Currency Exchange DescriptionSeveral currency exchange points are working in our city. Let us suppos ...
- POJ1860 Currency Exchange(bellman-ford)
链接:http://poj.org/problem?id=1860 Currency Exchange Description Several currency exchange points are ...
- POJ1860 Currency Exchange【最短路-判断环】
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- POJ1860:Currency Exchange(BF)
http://poj.org/problem?id=1860 Description Several currency exchange points are working in our city. ...
- poj1860 Currency Exchange(spfa判断正环)
Description Several currency exchange points are working in our city. Let us suppose that each point ...
- POJ1860 Currency Exchange —— spfa求正环
题目链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- POJ-1860 Currency Exchange( Bellman_Ford, 正环 )
题目链接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our ...
- [poj1860] Currency Exchange (bellman-ford算法)
题目链接:http://poj.org/problem?id=1860 题目大意:给你一些兑换方式,问你能否通过换钱来赚钱? 使用ford算法,当出现赚钱的时候就返回YES,如果不能赚钱,则返回NO ...
- POJ1860 Currency Exchange(最短路)
题目链接. 分析: 以前没做出来,今天看了一遍题竟然直接A了.出乎意料. 大意是这样,给定不同的金币的编号,以及他们之间的汇率.手续费,求有没有可能通过不断转换而盈利. 直接用Bellman-ford ...
- poj1860 Currency Exchange(spfa判断是否存在正环)
题意:有m个货币交换点,每个点只能有两种货币的互相交换,且要给佣金,给定一开始的货币类型和货币数量,问若干次交换后能否让钱增加. 思路:spfa求最长路,判断是否存在正环,如果存在则钱可以在环中一直增 ...
随机推荐
- web安全之如何防止CSRF跨站请求伪造
CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种对网站 ...
- HDU 4305 Lightning(计算几何,判断点在线段上,生成树计数)
Lightning Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Delphi DevExpress下载
http://download.csdn.net/detail/wozengcong/8395285#comment
- JavaScript 巧学巧用
关于 微信公众号:前端呼啦圈(Love-FED) 我的博客:劳卜的博客 知乎专栏:前端呼啦圈 前言 由于工作和生活上的一些变化,最近写文章的频率有点下降了,实在不好意思,不过相信不久就会慢慢恢复过来, ...
- hive删除列
hive中删除列时没有与mysql语句alter table <table> drop column <col>对应的语句. 然而依然可以完成此功能:使用ALTER TABLE ...
- ylbtech-LanguageSamples-PartialTypes(部分类型)
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-PartialTypes(部分类型) 1.A,示例(Sample) 返回顶部 “分部类型 ...
- unity shader 编译时间过长
去掉opengles2.0能省一半时间 换ssd Compiled shader 'Shader Forge/Scenes_Ground_Standard_M' in 315.51s gles ...
- TensorFlow------读取CSV文件实例
TensorFlow之读取CSV文件实例: import tensorflow as tf import os def csvread(filelist): ''' 读取CSV文件 :param fi ...
- CCControlExtension/CCControlButton
#ifndef __CCCONTROL_BUTTON_H__ #define __CCCONTROL_BUTTON_H__ #include "CCControl.h" #incl ...
- 第八章:SCRT搭建ES搜索引擎步骤
1.打开SecureCRT工具,输入服务器IP.端口号,确认后根据提示输入账号密码. 2.进入后判断服务器是否安装过JDK(1.6.0以上版本), 输入命令:# java –version 3.如 ...