题目链接: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. Linux部署禅道环境

    1.打开WinSCP 2.  输入Linux IP 用户名(root)及密码(123456)并点击保存 3.  点击登录后再输入一次密码 4.把ZenTaoPMS.11.2.stable.zbox_6 ...

  2. 用vue.js写的一个瀑布流的组件

    用vue.js写的一个瀑布流的组件:https://segmentfault.com/a/1190000010741319 https://www.jianshu.com/p/db3cadc03402

  3. ubuntu下修改子网掩码

    1.修改网络配置 修改 /etc/netplan/01-network-manager-all.yaml 文件 vi /etc/netplan/01-network-manager-all.yaml ...

  4. JS 自定义样式格式化日期

    Date.prototype.format = function (fmt) {             var o = {                 "M+": this. ...

  5. C#设计模式:工厂模式

    一,工厂模式 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  6. AI-IBM-cognitive class --Liner Regression

    Liner Regression import matplotlib.pyplot as plt import pandas as pd import pylab as pl import numpy ...

  7. elk相关启动脚本-shell编写

    elasticsearch-restart: #!/bin/bash PID=`ps -ef|grep elasticsearch|grep -vE 'grep|controller|elastics ...

  8. javaScript的预加载

    在有大量图片的页面中,为了避免页面加载完图片还未加载完成,我们通常会使用js的图片预加载. 这是一个预加载的demo: 首先把图片放入到一个类名为imgSrcArr的变量当中: var imgSrcA ...

  9. grep正则 以.o结尾的文件

    ls -l | grep *.o 查不出任何东西 . 代表一定有一个任意字符 * 重复零个到无穷多个前一个字符(所以需要前面有字符) 所以应该是 ls -l | grep '.*\.o' .*表示零个 ...

  10. 【串线篇】sql映射文件-分布查询(上)association 1-1

    1.场景 1把钥匙带1把锁 JavaBean:private Lock lock;//当前钥匙能开哪个锁: 1). interface KeyDao: public Key getKeyByIdSim ...