题目链接: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. 【洛谷p1036】选数

    (一定要声明我太蒟了,这个题扣了一上午……) 算法标签: …… dfs真的不是我所擅长的qwq,这道题的思路其实很简单,就是先dfs搜索所有可能的和,然后判断是不是质数.说着好说,然鹅并不好写: 第一 ...

  2. Sunday 字符串匹配算法(C++实现)

    简介: Sunday算法是Daniel M.Sunday于1990年提出的一种字符串模式匹配算法.其核心思想是:在匹配过程中,模式串并不被要求一定要按从左向右进行比较还是从右向左进行比较,它在发现不匹 ...

  3. JavaScript 中的 Function.prototype.bind() 方法

    转载自:https://www.cnblogs.com/zztt/p/4122352.html Function.prototype.bind()方法 bind() 方法的主要作用就是将函数绑定至某个 ...

  4. mpvue中的 钩子函数

    经过多次脱坑实验总结如下, 1,页面的A第一次加载会调用mounted钩子函数,onshow, 2,当从a->b,b再返回到a时,a不会触发mounted,而是触发onshow, 3,如果a中存 ...

  5. read - 在文件描述符上执行读操作

    概述#include <unistd.h> ssize_t read(int fd, void *buf, size_t count); 描述read() 从文件描述符 fd 中读取 co ...

  6. vue,一路走来(2)--路由vue-router

    安装 Mint UI cnpm install mint-ui --save 如果你的项目会用到 Mint UI 里较多的组件,最简单的方法就是把它们全部引入.此时需要在入口文件 main.js 中: ...

  7. 一、WebFrom 图片上传

    一.代码实现了简单的图片上传功能(改一下也可以上传其他的),没有做图片大小和格式的判断,主要是熟悉fileupload控件 前台代码: <%@ Page Language="C#&qu ...

  8. 逗号导致hive报“SemanticException Error in parsing”错误

    > select p.dt, p.cookie_qunar_global, p.refer_domain, p.kwid, p.query_word, p,traffic_type--, p.p ...

  9. 【抓包工具之Fiddler】增加IP列;session高亮

    Fiddler 在处理每个session时,脚本文件CustomRules.js中的方法都会运行,该脚本使得你可以隐藏,标识或任意修改负责的session.规则脚本在运行状态下就可以修改并重新编译,不 ...

  10. reids 持久化

    RDB: RDB是整个内存压缩过的Snapshot,RDB 的数据结构,可以配置符合的快照触发条件,默认如下 900s  1次修改 300s 10次修改 60s 10000 次修改 自动备份为dump ...