博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[kuangbin带你飞]专题四 最短路练习 E - Currency Exchange(判断负环)
阅读量:4557 次
发布时间:2019-06-08

本文共 4042 字,大约阅读时间需要 13 分钟。

E - Currency Exchang

题目链接:

题目:

Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency.
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 R
AB, C
AB, R
BA and C
BA - 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
The first line of the input contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=10
3.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10
-2<=rate<=10
2, 0<=commission<=10
2.
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 10
4.
Output
If Nick can increase his wealth, output YES, in other case output NO to the output file.
Sample Input
3 2 1 20.01 2 1.00 1.00 1.00 1.002 3 1.10 1.00 1.10 1.00
Sample Output
YES 题意:有n种货币可以相互转换,之间有汇率和手续费,问是否经过转换点的转换使得货币增多 思路:判断图是否存在负权环,用spfa算法可以高效判断是否存在负权环,只要发现一个点入队列次数大于n,则就存在负权环使得货币增加,因为一个点的入队次数一定小于等于(n-1)
#include 
#include
#include
#include
using namespace std;const int maxn=1005;int book[maxn],head[maxn],cnt[maxn];int pos,n,m,s;double d[maxn],v;struct Edge{ int to,next; double r,c;}edge[maxn];void add(int a,int b,double r,double c){ edge[pos].to=b; edge[pos].r=r; edge[pos].c=c; edge[pos].next=head[a]; head[a]=pos++;}bool spfa(){ memset(book,0,sizeof(book)); memset(d,0,sizeof(d)); memset(cnt,0,sizeof(cnt)); queue
qu; qu.push(s); book[s]=1; d[s]=v; cnt[s]++; while(!qu.empty()) { int now=qu.front(); qu.pop(); book[now]=0; for(int i=head[now];i!=-1;i=edge[i].next) { int toto=edge[i].to; double rr=edge[i].r; double cc=edge[i].c; if(d[toto]<(d[now]-cc)*rr) { d[toto]=(d[now]-cc)*rr; if(!book[toto]) { book[toto]=1; cnt[toto]++; qu.push(toto); } if(cnt[toto]>n) return true; } } } return false;}int main(){ scanf("%d%d%d%lf",&n,&m,&s,&v); pos=0; memset(head,-1,sizeof(head)); int a,b; double r,c; for(int i=0;i

 

 

转载于:https://www.cnblogs.com/Vampire6/p/11215515.html

你可能感兴趣的文章
linux-CentOS6.4下安装oracle11g详解
查看>>
实力为王 八年DBA经验谈
查看>>
2-sat 问题 【例题 Flags(2-sat+线段树优化建图)】
查看>>
ext3.2 右击动态添加node的treepanel
查看>>
Database links
查看>>
1035 插入与归并(25 分)
查看>>
STL中排序函数的用法(Qsort,Sort,Stable_sort,Partial_sort,List::sort)
查看>>
如何解决php 生成验证码图片不显示问题
查看>>
PHP,javascript实现大文件上传
查看>>
c#图像处理算法学习
查看>>
webApi之FromUri和FromBody区别
查看>>
【SoapUI】http接口测试
查看>>
各种工具网站
查看>>
数据库事务
查看>>
xe7 控件升级
查看>>
TFrame bug
查看>>
刚学习的如何才能自信的拍美美的婚纱照呢(要结婚啦)
查看>>
M51文件注释
查看>>
关于临界资源访问互斥量的死锁问题
查看>>
django-view层
查看>>