poj 1860 Currency Exchange 解题报告
题目链接:http://poj.org/problem?id=1860
题目意思:给出 N 种 currency, M种兑换方式,Nick 拥有的的currency 编号S 以及他的具体的currency(V)。M 种兑换方式中每种用6个数描述: A, B, Rab, Cab, Rba, Cba。其中,Rab: 货币A 兑换 货币B 的汇率为Rab,佣金为Cab。Rba:货币B 兑换 货币 A 的汇率,佣金为Cba。假设含有的A货币是x,那么如果兑换成B,得到的货币B 就是:(x-Cab) * Rab。问从 货币S 经过一定次数的兑换,最终回归到货币S,能否使得 Nick 本来含有的 S 大。
思路我是借鉴这个人的:
http://blog.csdn.net/lyhvoyage/article/details/19281013
可以说,是用了Bellman_ford 的 逆向思维。传统的Bellman_ford 是用来求可以含有负边权的最短路径,且判断是否有负权回路。
这个题目希望我们验证是否存在正权回路:顶点的权值能不断增加,且能无限一直松弛下去。
不过初始化与传统的Bellman_ford 是不同的, dist[S] = V,其他dist[i] = 0 / 无穷小。当 S 到其他点的 距离能不断增大时,说明存在正权回路。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = 1e4 + ;
const int maxv = + ; double has, dist[maxv];
int cnt, type;
int N, M; struct node
{
int a, b;
double rate, commission;
}currency[maxn]; bool Bellman_ford()
{
for (int i = ; i <= N; i++)
dist[i] = (i == type ? has : );
for (int i = ; i < N; i++)
{
bool flag = false;
for (int j = ; j < cnt; j++)
{
double t = (dist[currency[j].a] - currency[j].commission) * currency[j].rate;
if (t > dist[currency[j].b])
{
dist[currency[j].b] = (dist[currency[j].a] - currency[j].commission) * currency[j].rate;
flag = true;
}
}
if (!flag)
break;
}
for (int j = ; j < cnt; j++)
{
double t = (dist[currency[j].a] - currency[j].commission) * currency[j].rate;
if (t > dist[currency[j].b])
return true;
}
return false;
} int main()
{
while (scanf("%d%d%d%lf", &N, &M, &type, &has) != EOF)
{
int A, B;
double Rab, Cab, Rba, Cba;
cnt = ;
while (M--)
{
scanf("%d%d%lf%lf%lf%lf", &A, &B, &Rab, &Cab, &Rba, &Cba);
currency[cnt].a = A;
currency[cnt].b = B;
currency[cnt].rate = Rab;
currency[cnt].commission = Cab; currency[++cnt].a = B;
currency[cnt].b = A;
currency[cnt].rate = Rba;
currency[cnt++].commission = Cba;
}
printf("%s\n", Bellman_ford() ? "YES" : "NO");
}
return ;
}
poj 1860 Currency Exchange 解题报告的更多相关文章
- 最短路(Bellman_Ford) POJ 1860 Currency Exchange
题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
- POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告
三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...
- POJ 1860 Currency Exchange 最短路+负环
原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- POJ 1860 Currency Exchange【bellman_ford判断是否有正环——基础入门】
链接: http://poj.org/problem?id=1860 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- poj - 1860 Currency Exchange Bellman-Ford 判断正环
Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...
- POJ 1860 Currency Exchange (Bellman-Ford)
题目链接:POJ 1860 Description Several currency exchange points are working in our city. Let us suppose t ...
随机推荐
- *AtCoder Grand Contest 002F - Leftmost Ball
$n \leq 2000,k \leq 2000$,现$n$种球每种有$k$个,在一种排列中,会把每种颜色的球第一个出现的涂成第0种(不同于原来的n种)颜色,问最终会出现多少种不同的序列.膜1e9+7 ...
- GT考试 BZOJ 1009
GT考试 [问题描述] 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字. 他的不吉利数学A1A2...Am(0< ...
- 更全的bootstrap教程连接
更全的bootstrap教程: http://www.jb51.net/article/84087.htm
- 在 VirtualBox 5.0 系列中让虚拟机支持 USB 3.0 必须开启 APIC
VirtualBox 5.0 系列正式支持 USB 3.0,能够在宿主机支持 USB 3.0 的情况下,让虚拟机也选择具备 USB 3.0 的功能.但是经过多方试验,发现必须在 VirtualBox ...
- HDU 1394 线段树求逆序对
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 洛谷—— P2656 采蘑菇
https://www.luogu.org/problem/show?pid=2656 题目描述 小胖和ZYR要去ESQMS森林采蘑菇. ESQMS森林间有N个小树丛,M条小径,每条小径都是单向的,连 ...
- XCTFreverse第一道DMD
MD5解一次密即可
- org.apache.catalina.connector.ClientAbortException: java.io.IOException: APR error:-32
org.apache.catalina.connector.ClientAbortException: java.io.IOException: APR error:-32 Most likely, ...
- SpringCloud中Rabbitmq的使用
1.pom配置,添加以来jar包 <dependency> <groupId>org.springframework.cloud</groupId> <art ...
- linux cat 文件操作
简略版: cat主要有三大功能:1.一次显示整个文件.$ cat filename2.从键盘创建一个文件.$ cat > filename 只能创建新文件,不能编辑已有文件.3.将几个文 ...