题目链接: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)的更多相关文章

  1. 最短路(Bellman_Ford) POJ 1860 Currency Exchange

    题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...

  2. POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)

    POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...

  3. POJ 1860 Currency Exchange 最短路+负环

    原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...

  4. POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告

    三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...

  5. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) T ...

  6. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  7. POJ 1860 Currency Exchange【bellman_ford判断是否有正环——基础入门】

    链接: http://poj.org/problem?id=1860 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  8. POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  9. poj - 1860 Currency Exchange Bellman-Ford 判断正环

    Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...

随机推荐

  1. mybatis输出映射总结

    使用resultType作为输出映射 只有查询出来的列名和pojo中的属性名一致时,才会映射成功, 如果不一致,可以使用resultMap最为输出类型 不管是输出单个对象还是列表(list中包括poj ...

  2. Spring Cloud Stream 进行服务之间的通讯

    Spring Cloud Stream Srping cloud Bus的底层实现就是Spring Cloud Stream,Spring Cloud Stream的目的是用于构建基于消息驱动(或事件 ...

  3. P5030 长脖子鹿放置

    题目背景 众周所知,在西洋棋中,我们有城堡.骑士.皇后.主教和长脖子鹿. 题目描述 如图所示,西洋棋的"长脖子鹿",类似于中国象棋的马,但按照"目"字攻击,且没 ...

  4. Redis数据库-基础篇

    Redis Redis是一个开源的,先进的key-value存储. 它通常被称为数据结构服务器,因为键可以包含字符串,哈希,链表,集合和有序集合. Redis 简介 Redis 是完全开源免费的,遵守 ...

  5. JS :Date日期格式化

    Date.prototype.format = function (formatStr) { var date = this; /* 函数:填充0字符 参数:value-需要填充的字符串, lengt ...

  6. 并行开发 1.Parallel

    原文:8天玩转并行开发——第一天 Parallel的使用 随着多核时代的到来,并行开发越来越展示出它的强大威力,像我们这样的码农再也不用过多的关注底层线程的实现和手工控制, 要了解并行开发,需要先了解 ...

  7. C#面试 笔试题 三

    1.传入某个属性的set方法的隐含参数的名称是什么? value,它的类型和属性所声名的类型相同.   2.C#支持多重继承么? 类之间不支持,接口之间支持.类对接口叫做实现,不叫继承. 3.C#中所 ...

  8. k3 cloud成本调整单引入单据后,再做出库成本核算。成本调整单列表已审核的单据消失,非已审核的单据还在,这是出库成本核算设置参数的问题吗?

    存货核算时,会将“期末余额调整”类型的的调整单删除后,再重新产生:因此引入后不要再做出库核算,或者引入其它类型的单据.

  9. python 子类继承父类__init__(转载)

    转载: http://www.jb51.net/article/100195.htm 前言 使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,__init__方法 ...

  10. lambda 分组后的count

    var list = stuList.GroupBy(b => b.PersonalId).Select(g => (new { personalId = g.Key, count = g ...