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 ...
随机推荐
- Java 引用类型变量的声明和使用
引用类型变量的声明和使用 (1)把类名当作是一种类型来声明变量,这种变量叫引用类型变量.如:People people; (2)引用类型变量保存对象的“引用”,即对象的地址. (3)对象的创建 new ...
- SpringMvc入门四----rest风格Url
知识点: REST风格URL简介 SpringMvc对rest风格的支持 @PathVariable 获取 Url 变量 SpringMvc对静态资源的处理 REST风格URL简介: 我们平时看到的s ...
- FPGA简单概述
中国FPGA市场及人才需求 如今毕业生的就业问题十分严峻,特别是计算机专业的毕业生.通过对FPGA目前发展情况的分析和市场岗位的调查,结合高职高专生在FPGA岗位的定位,制定相应的教学计划,使高职高专 ...
- 南阳OJ 1170 最大的数
最大的数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 小明和小红在打赌说自己数学学的好,于是小花就给他们出题了,考考他们谁NB,题目是这样的给你N个数 在这n个数 ...
- 【转】使用Jmeter测试Webservice简单示例
1.webservice 先简单开发webservice,参考文档 http://www.cnblogs.com/xwdreamer/archive/2011/12/07/2296914.html w ...
- python开发mysql:mysql数据类型&约束条件
一 整形 只有Int类型跟存储没有关系,显示的是宽度,其他类型都是限制 整形类型:[(m)][unsigned][zerofill] 作用:存储年龄,等级,id,各种号码 m,代表显示宽度 默认11 ...
- Linux学习笔记 -- 硬链接与软连接(转)
原文地址: http://www.cnblogs.com/itech/archive/2009/04/10/1433052.html Linux链接概念 Linux链接分两种,一种被称为硬链接(Har ...
- 如何成为java架构师(转载)
链接:https://www.zhihu.com/question/29031276/answer/54631312 来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 1 ...
- C++11 变量和函数的链接性
在全局变量前添加const或者static,则该变量链接性为内部,即文件内有效.可以使用extern声明为外部. 如果要让函数的链接性为内部,则函数声明和定义都应使用static关键字. 例子: te ...
- 命令提示符(cmd)中的tracert命令详解(小技巧)
tracert也被称为Windows路由跟踪实用程序,在命令提示符(cmd)中使用tracert命令可以用于确定IP数据包访问目标时所选择的路径.本文主要探讨了tracert命令的各个功能. 百度经验 ...