poj1860 Currency Exchange(spfa判断正环)
Description
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
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
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个兑换点,每个兑换点可以在收取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<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; struct node
{
int x;
double r,c;
}; vector<node> g[];
double d[],c;
int vis[],cnt[];
int n,m,s; int check(int u)
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
d[i]=;
}
d[u]=c;
queue<int> q;
q.push(u);
while(!q.empty())
{
int x=q.front();
int sz=g[x].size();
vis[x]=;
q.pop();
for(int i=;i<sz;i++)
{
int y=g[x][i].x;
double r=g[x][i].r,c=g[x][i].c;
if((d[x]-c)*r>d[y])
{
d[y]=(d[x]-c)*r;
cnt[y]++;
if(!vis[y])
{
q.push(y);
vis[y]=;
if(cnt[y]==n)
{
return ;
}
}
}
}
}
return ;
} int main()
{
scanf("%d%d%d%lf",&n,&m,&s,&c);
for(int i=;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");
}
}
poj1860 Currency Exchange(spfa判断正环)的更多相关文章
- POJ1860 Currency Exchange —— spfa求正环
题目链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- poj1860 Currency Exchange(spfa判断是否存在正环)
题意:有m个货币交换点,每个点只能有两种货币的互相交换,且要给佣金,给定一开始的货币类型和货币数量,问若干次交换后能否让钱增加. 思路:spfa求最长路,判断是否存在正环,如果存在则钱可以在环中一直增 ...
- poj - 1860 Currency Exchange Bellman-Ford 判断正环
Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...
- POJ1680 Currency Exchange SPFA判正环
转载来源:優YoU http://user.qzone.qq.com/289065406/blog/1299337940 提示:关键在于反向利用Bellman-Ford算法 题目大意 有多种汇币,汇 ...
- Currency Exchange POJ - 1860 (spfa判断正环)
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- Currency Exchange POJ - 1860 spfa判断正环
//spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace s ...
- poj3621 SPFA判断正环+二分答案
Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big c ...
- HDU 1317(Floyd判断连通性+spfa判断正环)
XYZZY Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)
XYZZY Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
随机推荐
- zabbix 3.0.2自定义脚本
http://blog.51cto.com/xiao987334176/1769766 有一个通知队列,如果超过了一定的值,就需要报警一下 查询接口可以返回队列的数量,格式是json,data后面的数 ...
- php-excel 与 laravel-excel
php-excel: http://blog.csdn.net/evkj2013/article/details/65441170 php-excel中文手册(前端的jqury) http://www ...
- HTMLTestRunner生成报告 中文展示乱码的问题
- Java-Maven-Runoob:Maven 自动化构建
ylbtech-Java-Maven-Runoob:Maven 自动化构建 1.返回顶部 1. 自动化构建定义了这样一种场景: 在一个项目成功构建完成后,其相关的依赖工程即开始构建,这样可以保证其依赖 ...
- 解决sever 2008中tomcat的报错 init Failed to initialize end point associated with ProtocolHandler ["http-nio-80"]
错误现象: 01-Aug-2017 14:59:50.140 信息 [main] org.apache.coyote.AbstractProtocol.init Initializing Protoc ...
- adb正常,手机启动usb调试,adb devices下没有改设备
手机开启开发者模式,adb正常时adb devices下没有设备: 1.进入设备管理器--查找adb的硬件id
- Python多线程-队列
队列就是存东西取东西,多用于多线程中 按照顺序 对传入的数据按规定的顺序输出 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" ...
- PHP文件操作(二)-文件的读取
1.fread() //读取打开的文件 fread(file,length) file:必选项,规定要读取的打开的文件 length:必选项,规定要读取的最大字节数. <?php $fil ...
- QR 码的位置检测符
QR码的位置检测符由三个同心正方形叠加而成.分别为: 7*7 modules的黑色正方形: 5*5 modules的白色正方形 : 3*3modules的黑色正方形. 三个用于定位检测的“回”形符号应 ...
- component to string 自定义窗体
component to string string to component StringToComponent ComponentToString ObjectTextToBinary Objec ...