设为首页 收藏本站
查看: 804|回复: 0

[经验分享] POJ 1860:Currency Exchange(Bellman_Ford算法)

[复制链接]

尚未签到

发表于 2015-11-24 14:42:46 | 显示全部楼层 |阅读模式
Currency Exchange

Time Limit: 1000MS
Memory Limit: 30000K
Total Submissions: 12217
Accepted: 4120


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


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种货币,同时有M个货币兑换站点,每个站点支持两种货币A,B之间的相互兑换,六个数字(A,B,Rab,Cab,Rba,Cba)表示它的属性,假如A->B,则A的量Va可换得的B货币数Vb则,Vb=(Va-Cab)*Rab;先有一个含有s种货币V元,问他能否经过各种兑换后,换得的s货币多于V元;

Bellman-Ford算法描述:
  1,.初始化:将除源点外的所有顶点的最短距离估计&#20540; d[v] ←&#43;∞, d ←0;
  2.迭代求解:反复对边集E中的每条边进行松弛操作,使得顶点集V中的每个顶点v的最短距离估计&#20540;逐步&#36924;近其最短距离;(运行|v|-1次)
  3.检验负权回路:判断边集E中的每一条边的两个端点是否收敛。如果存在未收敛的顶点,则算法返回false,表明问题无解;否则算法返回true,并且从源点可达的顶点v的最短距离保存在 d[v]中。  描述性证明:
  首先指出,图的任意一条最短路径既不能包含负权回路,也不会包含正权回路,因此它最多包含|v|-1条边。
  其次,从源点s可达的所有顶点如果 存在最短路径,则这些最短路径构成一个以s为根的最短路径树。Bellman-Ford算法的迭代松弛操作,实际上就是按顶点距离s的层次,逐层生成这棵最短路径树的过程。

源代码:(0MS)
#include<iostream>
using namespace std;
#define eps 1e-9
#define MAX_N  105
struct edge
{
int u;
int v;
double rate;
double cost;
}edge[2*MAX_N];
int N,M,s,acrNum;
double V;
bool Bellman_Ford()
{
bool flag;//判断能否收敛
int i;
double d[MAX_N];
for(i=1;i<=N;i++)
d = 0.0;
d = V;
while(d <= V+eps)
{
flag = true;
for(i=0;i<acrNum;i++)
{
double tmp = (d[edge.u] - edge.cost)*edge.rate;
if(d[edge.v] + eps < tmp)
{
flag = false;
d[edge.v] = tmp;
}
}
if(flag)//没有收敛,则是无法找到
return (d-V) > 0;
}
return 1;
}
int main()
{
while(scanf(&quot;%d%d%d%lf&quot;,&N,&M,&s,&V)!=EOF)
{
int A,B;
double Rab,Cab,Rba,Cba;
for(int i=0;i<M;i++)
{
scanf(&quot;%d%d%lf%lf%lf%lf&quot;,&A,&B,&Rab,&Cab,&Rba,&Cba);
//A->B
edge.u = A;edge.v = B;
edge.rate = Rab;edge.cost = Cab;
//B->A
edge[i+M].u = B;edge[i+M].v = A;
edge[i+M].rate = Rba;edge[i+M].cost = Cba;
}
acrNum = 2*M;
if(Bellman_Ford())printf(&quot;YES\n&quot;);
elseprintf(&quot;NO\n&quot;);
}
return 0;
}





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-143159-1-1.html 上篇帖子: C++11 CAS无锁函数compare_exchange_weak的使用 下篇帖子: 如何从计算机中彻底删除 Exchange Server 2003
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表