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 ...
随机推荐
- TXT
ANDRIOD: 192.168.199.119 data50803360 zc_3floor kk4836kk kahuna kk1626kk
- 导出csv xls文件数字会自动变科学计数法的解决方式
将数据导出excel文件变成科学计数法问题: 分析: 用程序导出的csv文件,当字段中有比较长的数字字段存在时,在用excel软件查看csv文件时就会变成科学技术法的表现形式. 其实这个 ...
- Python 自动化测试config配置文件ini 配置目录
import ConfigParserimport os path = os.path.join(os.path.dirname(__file__), 'config.ini').replace('\ ...
- 分布式缓存系统 Memcached CAS协议
Memcached在1.2.4版本后新增了CAS(Check and Set)协议,主要用于并发控制:memcached中同一个item同时被多个线程(多个客户端)更改的并发问题.CAS协议最本质的东 ...
- 1048 Find Coins
题意:略 思路:two pointers思想,简单 先对数字序列进行排序,然后定义两个指针left和right,初始状态low=0,high=n-1.当a[low]+a[high] > M时,h ...
- Redis存储AccessToken
AccessToken 2小时有效. 就不要每次都调取了,这样会造成浪费. 或者存入Session中,设置过期时间. 或者存入Redis中,设置过期时间. 过期之后,进行重新获取. <?php ...
- 从零开始搭建包含多个子系统的Vue工程项目
本文以windows为例,介绍支持多个子系统的Vue工程项目的搭建过程,相对于单一系统的工程,多个子系统引入了如下一些问题: 项目目录结构设计 打包结果设计:每个子系统可以独立发布上线 多布局实现:多 ...
- 使用原生js自定义内置标签
使用原生js自定义内置标签 效果图 代码 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- burpsuite扫描web目录
1.进行抓包 2.将其发送到lntruder 3.使用替换脚本替换掉/ 4.替换 5.替换结果 6.将多余的$$删除,在/后面添加$$ //$$就是payload 7.测试结果 替换脚本代码: ...
- NUnit属性
TestFixture:它标记一个类包含测试,申明该类是用来测试的.一般用在class的定义之前: Test一般是放在method之前,表示对该方法的测试:如前一篇文章所示的class. SetUp/ ...