POJ1860 Currency Exchange(bellman-ford)
链接:http://poj.org/problem?id=1860
Currency Exchange
Description
For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 - 0.39) * 29.75 = 2963.3975RUR.
You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B - numbers of currencies it exchanges, and real RAB, CAB, RBA and CBA - exchange rates and commissions when exchanging A to B and B to A respectively.
Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations.
Input
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102.
Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 104.
Output
Sample Input
3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00
Sample Output
YES 经过n-1次循环还能继续增大的情况说明有环,就一定能达到足够大,可以在回到起始点的时候还能增值
代码:
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
double dist[MAXN];
struct Edge
{
int u,v;
double r,c;
};
vector<Edge> E;
bool bellman_ford(int start,int n,double val)//点的编号从1开始
{
for(int i=;i<=n;i++)dist[i]=;
dist[start]=val;
for(int i=;i<n;i++)//最多做n-1次
{
bool flag=false;
for(int j=;j<E.size();j++)
{
int u=E[j].u;
int v=E[j].v;
double r=E[j].r;
double c=E[j].c;
if(dist[v]<(dist[u]-c)*r)
{
dist[v]=(dist[u]-c)*r;
flag=true;
}
}
if(!flag)return false;
}
for(int j=;j<E.size();j++)
if(dist[E[j].v]<(dist[E[j].u]-E[j].c)*E[j].r)
return true;
return false;
}
int main()
{
int n,m;
int start;
double val;
int a,b;
int i;
double r,c;
Edge e;
scanf("%d%d%d%lf",&n,&m,&start,&val);
E.clear();
for(i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
scanf("%lf%lf",&r,&c);
e.u=a;e.v=b;e.r=r;e.c=c;
E.push_back(e);
scanf("%lf%lf",&r,&c);
e.u=b;e.v=a;e.r=r;e.c=c;
E.push_back(e);
}
if(bellman_ford(start,n,val))
printf("YES\n");
else
printf("NO\n");
return ;
}
POJ1860 Currency Exchange(bellman-ford)的更多相关文章
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit : 2000/1000ms (Java/Other) Memory Limit : 60000/30000K (Java/Other) T ...
- Currency Exchange(最短路)
poj—— 1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29851 Ac ...
- poj1860 兑换货币(bellman ford判断正环)
传送门:点击打开链接 题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...
- POJ 1860 Currency Exchange(SPFA+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
- poj1860(Bellman—fold)
题目连接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our ...
- POJ 1860 Currency Exchange (bellman-ford判负环)
Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several c ...
- POJ 2240 Arbitrage (Bellman Ford判正环)
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions:27167 Accepted: 11440 Descri ...
- POJ 1860 Currency Exchange(如何Bellman-Ford算法判断图中是否存在正环)
题目链接: https://cn.vjudge.net/problem/POJ-1860 Several currency exchange points are working in our cit ...
随机推荐
- js 实现图片实时预览
<body> 上传图片: <input type="file" name="file" style="width: 200px; h ...
- struts2 No result defined for action XXX and result input
这种错误的原因一般是页面的属性和action里的属性个数.名称.类型不一致造成的 困扰了我一下午的问题,原来是表单中有两个input-text的name属性重复了,然后接受参数的时候就出现了这个错误 ...
- Echarts 3.19 制作常用的图形 非静态
最近阿里内部使用的 图表也向外开放了 而百度就好像更有良心一点,Echarts 早就开放了 . 自己学Echarts的时候走了很多的弯路,毕竟谁让自己菜呢,多撞几次南墙才晓得疼 才知道学习方法,新手上 ...
- 解决绝对定位div position: absolute 后面的<a> Link不能点击
今天布局的时候,遇到一个bug,当DIV设置为绝对定位时,这个div后面的相对定位的层里面的<a>Link标签无法点击. 网上的解决方案是在绝对定位层里面添加:pointer-events ...
- Jsonp调用网易云音乐API搜索播放歌曲
效果如下图: 基本就是正常的文件播放,暂停,停止,设置循环,随机播放,加速,减速,上一曲,下一曲,再多个选择本地文件加入到播放列表的功能.然后想着给加个能搜索网络歌曲并且播放的功能,今天研究了一下,成 ...
- C/C++调试工具gdb
关于Gdb的使用,请参考:http://blog.csdn.net/haoel/article/details/2879
- ***CodeIgniter集成微信支付(转)
微信支付Native扫码支付模式二之CodeIgniter集成篇 http://www.cnblogs.com/24la/p/wxpay-native-qrcode-codeigniter.html ...
- PHP 版本判断 version_compare() 函数
在度娘中简单的找了下,判断当前PHP的版本是否高于某个版本,或者低于某个版本的方法 显示的结果基本上都是一样的,好吧,要不是我忘记了version_compare()这个函数我才不会去找度娘,果断找以 ...
- 阿里云服务器上开启linux远程桌面连接
一.说明: 本文的目的是实现在windows机器上利用远程桌面连接来访问远程的linux桌面. 这里使用的是阿里云服务器,操作系统为Centos6.5. 二.基本步骤: 1.首先保证服务器已经安装完毕 ...
- ABP中单元测试的技巧:Mock和数据驱动
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:虽然ABP为大家提供了测试的脚手架了,不过有些小技巧还是需要自己探索的. ASP.NE ...