poj 1860 Currency Exchange
2016-05-23 00:13
295 查看
Currency Exchange
Description
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 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
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<=103.
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
If Nick can increase his wealth, output YES, in other case output NO to the output file.
Sample Input
Sample Output
Source
Northeastern Europe 2001, Northern Subregion
这道题其实就是询问是否可以形成正权环;
用Bellman_Ford算法即可解决问题;
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 25315 | Accepted: 9245 |
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 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
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<=103.
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
If Nick can increase his wealth, output YES, in other case output NO to the output file.
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
Source
Northeastern Europe 2001, Northern Subregion
这道题其实就是询问是否可以形成正权环;
用Bellman_Ford算法即可解决问题;
#include<iostream> #include<algorithm> #include<string> #include<cstring> using namespace std; struct Edge { int from; int to; double rate; double fee; }edge[205]; int N,M,S; double V; double dis[105]; bool relax(int start) { double t=(dis[edge[start].from]-edge[start].fee)*edge[start].rate; if(t>dis[edge[start].to]) { dis[edge[start].to]=t; return true; } return false; } bool BellmanFord() { bool flag; for(int i=1;i<=N;++i) { dis[i]=0; } dis[S]=V; for(int i=1;i<=N;++i) { flag=false; for(int j=1;j<=2*M;++j) { if(relax(j)) { flag=true; } } if(!flag) break; } for(int i=1;i<=2*M;++i) { if(relax(i)) return true; } return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin>>N>>M>>S>>V; int from,to; double r1,c1,r2,c2; for(int i=1;i<=M;++i) { cin>>from>>to>>r1>>c1>>r2>>c2; edge[2*i-1].from=from; edge[2*i-1].to=to; edge[2*i-1].rate=r1; edge[2*i-1].fee=c1; edge[2*i].from=to; edge[2*i].to=from; edge[2*i].rate=r2; edge[2*i].fee=c2; } bool flag=BellmanFord(); if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl; return 0; }
相关文章推荐
- H264 数据avi文件封装和拆解
- ThreadPoolExecutor里面4种拒绝策略(详细)
- Spring-data-redis:特性与实例
- 什么时候使用覆盖操作
- 正则表达式——肯定环视模拟固化分组
- #pragma_pack(n)_与__attribute(aligned(n))的关系与区别
- 享元模式
- Spark 定制版:006~Spark Streaming源码解读之Job动态生成和深度思考
- 继承,继承的好处
- struts-2小练习
- 2286: [Sdoi2011消耗战
- 工作队列
- 兄台息怒,关于arguments,您的想法和大神是一样一样的----闲聊JS中的apply和call
- android 简单的网格布局 GridLayout
- leetcode_c++:Combination Sum_2(040)
- Untiy3D的粒子系统介绍
- [Java] 作业6(字符串)
- Linux08--Shell程序设计03 shell script
- cqoi2016
- ActionBar 用法