POJ 1860 Currency Exchange (Bellman-Ford)
题目链接:POJ 1860
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.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00
Sample Output
YES
Source
Northeastern Europe 2001, Northern Subregion
Solution
题意
有 \(n\) 种货币,给出一些两种货币之间的汇率及税价。
求原来持有的货币能否通过一些兑换过程使得价值增加。
思路
把货币看成结点,兑换的过程看成有向边,那么其实问题就是判断图中是否存在正环。
使用 \(Bellman-Ford\) 算法,与判断负环的方法类似,改变一下松弛的条件即可。注意初始化也需要修改。
Code
#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 1e3;
const double eps = 1e-8;
int n, m, s;
double v;
int tot;
double dis[maxn];
struct Edge {
int from, to;
double r, c;
Edge(int f = 0, int t = 0, double r = 0, double c = 0): from(f), to(t), r(r), c(c) {}
} edges[maxn];
void add(int f, int t, double r, double c) {
edges[tot++] = Edge(f, t, r, c);
}
bool Bellman_Ford() {
memset(dis, 0, sizeof(dis));
dis[s] = v;
for(int i = 1; i <= n - 1; ++i) {
bool flag = false;
for(int j = 0; j < tot; ++j) {
int f = edges[j].from, t = edges[j].to;
double r = edges[j].r, c = edges[j].c;
double tmp = (dis[f] - c) * r;
if(dis[t] < tmp) {
dis[t] = tmp;
flag = true;
}
}
if(!flag) {
break;
}
}
for(int i = 0; i < tot; ++i) {
if(dis[edges[i].to] < (dis[edges[i].from] - edges[i].c) * edges[i].r) {
return true;
}
}
return false;
}
int main() {
while(~scanf("%d%d%d%lf", &n, &m, &s, &v)) {
tot = 0;
int f, t;
double r1, c1, r2, c2;
for(int i = 0; i < m; ++i) {
scanf("%d%d%lf%lf%lf%lf", &f, &t, &r1, &c1, &r2, &c2);
add(f, t, r1, c1);
add(t, f, r2, c2);
}
if(Bellman_Ford()) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}
POJ 1860 Currency Exchange (Bellman-Ford)的更多相关文章
- 最短路(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 最短路+负环
原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告
三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit : 2000/1000ms (Java/Other) Memory Limit : 60000/30000K (Java/Other) T ...
- 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——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- poj - 1860 Currency Exchange Bellman-Ford 判断正环
Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...
随机推荐
- Lua中C API栈操作
向栈中压入数据: lua_pushnil(lua_State*); lua_pushboolean(lua_State*, bool); lua_pushnumber(lua_State*, lua_ ...
- [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 (Treap+单调队列)
题面 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个"群".每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi( ...
- go 文件读写
go 文件读写有很多方式 ioutil读文件 package main import ( "io/ioutil" "fmt" ) func main() { d ...
- Codeforces - 1194C - From S To T - 子序列 - 排序
https://codeforces.com/contest/1194/problem/C 好像没什么好说的,要能构造s必须是t的子序列,并且相差的字符集合d是p的子集. 用双指针法求两遍子序列就可以 ...
- 一个简单的winform程序调用webservices
本文原创,如需转载,请标明源地址,谢谢合作!http://blog.csdn.net/sue_1989/article/details/6597078 本文的编写IDE为VSTS2008和.NET F ...
- 快速的统计千万级别uv
菜菜,咱们网站现在有多少PV和UV了? Y总,咱们没有统计pv和uv的系统,预估大约有一千万uv吧 写一个统计uv和pv的系统吧 网上有现成的,直接接入一个不行吗? 别人的不太放心,毕竟自己写的,自己 ...
- 不小心执行 rm -f,该如何恢复?
每当我们在生产环境服务器上执行rm命令时,总是提心吊胆的,因为一不小心执行了误删,然后就要准备跑路了,毕竟人不是机器,更何况机器也有 bug,呵呵. 那么如果真的删除了不该删除的文件,比如数据库.日志 ...
- QT + openssl + VS2015静态编译
从http://slproweb.com/products/Win32OpenSSL.html下载已经编译好的openssl,一路next 我将OpenSSL-Win32\lib\VC目录下的libe ...
- Sass函数:unit()函数
unit() 函数主要是用来获取一个值所使用的单位,碰到复杂的计算时,其能根据运算得到一个“多单位组合”的值,不过只充许乘.除运算: >> unit(100) "" & ...
- Go 使用 append 向切片增加元素
1.// 创建一个整型切片 // 其长度和容量都是 5 个元素 slice := []int{10, 20, 30, 40, 50} // 创建一个新切片 // 其长度为 2 个元素,容量为 4 个元 ...