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

[经验分享] Currency Exchange(最短路)

[复制链接]

尚未签到

发表于 2017-7-1 20:40:09 | 显示全部楼层 |阅读模式
                           poj—— 1860 Currency Exchange


Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 29851 Accepted: 11245

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
题目大意:




   题目中主要是说存在货币兑换点,然后现在手里有一种货币,要各种换来换去,最后再换回去的时候看能不能使原本的钱数增多,每一种货币都有对应的汇率,而货币A到货币B的汇率即为1货币A换得得货币B的数量,但兑换点是要收取佣金的,且佣金从源货币中扣除,例如,你想在汇率29.75,佣金为0.39的兑换点把100美元换成卢布,得到的卢布数即为(100-0.39)*29.75 = 2963.3975.
  样例解释:

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
     多组输入,第一行中N代表有N种货币可以互相兑换,M代表有M个货币兑换点,S代表这个人手中的的货币的编号,V代表这个人手中拥有的货币数量,底下M行
  每行六个数,A,B代表可以交换的货币A和B,剩下的实数RAB,CAB,RBA,CBA,代表A到B的汇率,佣金,B到A的汇率,佣金。以某种兑换方式增加原本的钱数,而且必须兑换为原来的货币。




思路:

货币的交换是可以重复多次的,所以我们需要找出是否存在正权回路,且最后得到的s金额是增加的。





这一道题虽然需要求的是正权回路,但相应的这道题当中我们需要求的是最长路径,因此和Bellman-Ford算法中判断负环是类似的。

因此,我们只需要修改原本的松弛条件,然后先进行n-1轮松弛,最后再进行一次松弛作为检测存不存在正环就可以了。



链接地址:(参考博客)   http://blog.csdn.net/dgghjnjk/article/details/51684154

代码:




#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 1010
using namespace std;
int n,m,head[N],sum[N],tot,num,u,v;
bool vis[N];
double dis[N],x,y,xx,yy,money;
struct Edge
{
int u,v,next;
double x,y;
}edge[N<<1];
int add(int u,int v,double x,double y)//注意:传上来的是double类型的,开始时一直传的是int导致wa
{
tot++;//从1开始用,开始时一直是在后面++,导致一直wa
edge[tot].u=u;
edge[tot].v=v;
edge[tot].x=x;
edge[tot].y=y;
edge[tot].next=head;
head=tot;
}
void begin()
{
memset(head,0,sizeof(head));
memset(sum,0,sizeof(sum));
memset(dis,0,sizeof(dis));
memset(vis,false,sizeof(vis));
tot=0;
}
int spfa(int s)
{
queue<int>q;
dis=money;
vis=true;
q.push(s);
while(!q.empty())
{
int x=q.front();
q.pop();vis[x]=false;
for(int i=head[x];i;i=edge.next)
{
int v=edge.v;
if(dis[v]<(dis[x]-edge.y)*edge.x)
{
dis[v]=(dis[x]-edge.y)*edge.x;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
sum[v]++;
if(sum[v]>n)
return -1;
}
}
}
return 1;
}
int main()
{
while(scanf("%d %d %d %lf",&n,&m,&num,&money)!=EOF)
{
begin();
for(int i=1;i<=m;i++)
{
scanf("%d %d %lf %lf %lf %lf",&u,&v,&x,&y,&xx,&yy);
add(u,v,x,y);
add(v,u,xx,yy);
}
if(spfa(num)>0) printf("NO\n");
else printf("YES\n");
}
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-390190-1-1.html 上篇帖子: RabbitMQ 三种Exchange 下篇帖子: Exchange端口列表
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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