博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1860 Currency Exchange(spfa判断正环)
阅读量:4949 次
发布时间:2019-06-11

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

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.01 2 1.00 1.00 1.00 1.002 3 1.10 1.00 1.10 1.00

Sample Output

YES 题意:有n种货币,有m个兑换点,每个兑换点可以在收取Cab(币种为A)佣金(佣金固定!)后将A按Rab汇率换成B,也可在收取Cba(币种为B)佣金后将B按Rab汇率换成A,你现在有币种为S(我就不说S币了)的货币V元 请问你是否可以通过某些兑换将S换成其他货币,再换回S以获得更多的钱? 题解:很明显的一道spfa求正环,bfs方法中只需要一个点进队超过n次即构成正环     求正环吗,自然求的是最长路,而且d[x]+w>y也要改成d[x]*w>y,这也是常识了 代码如下:
#include
#include
#include
#include
#include
#include
using namespace std;struct node{ int x; double r,c;};vector
g[110];double d[110],c;int vis[110],cnt[110];int n,m,s;int check(int u){ memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) { d[i]=0; } d[u]=c; queue
q; q.push(u); while(!q.empty()) { int x=q.front(); int sz=g[x].size(); vis[x]=0; q.pop(); for(int i=0;i
d[y]) { d[y]=(d[x]-c)*r; cnt[y]++; if(!vis[y]) { q.push(y); vis[y]=1; if(cnt[y]==n) { return 1; } } } } } return 0;}int main(){ scanf("%d%d%d%lf",&n,&m,&s,&c); for(int i=1;i<=m;i++) { int f,t; double rab,cab,rba,cba,c1,c2; scanf("%d%d%lf%lf%lf%lf",&f,&t,&rab,&cab,&rba,&cba); node hehe; hehe.x=t; hehe.r=rab; hehe.c=cab; g[f].push_back(hehe); hehe.x=f; hehe.r=rba; hehe.c=cba; g[t].push_back(hehe); } int ans=check(s); if(ans) { printf("YES\n"); } else { printf("NO\n"); }}

 

 

转载于:https://www.cnblogs.com/stxy-ferryman/p/8438350.html

你可能感兴趣的文章
西门子_TDC_数据耦合小经验
查看>>
接口测试与postman
查看>>
LINQ To XML的一些方法
查看>>
[LeetCode] Copy List with Random Pointer
查看>>
openstack部署之nova
查看>>
JS组件系列——表格组件神器:bootstrap table
查看>>
存储过程Oracle(一)
查看>>
log4j日志归档
查看>>
Java笔记01——IO流
查看>>
mysql遇见error,1049
查看>>
NYOJ311 完全背包
查看>>
codevs——2822 爱在心中
查看>>
Python基础班---第一部分(基础)---Python基础知识---认识Python
查看>>
JAVA MAC 配置
查看>>
1134 最长上升子序列 (序列型 DP)
查看>>
js冒泡排序
查看>>
第一次作业 4班卢炳武
查看>>
抽象类的调用
查看>>
使用硬盘,安装双系统,Win7+CentOS
查看>>
Javascript学习总结
查看>>