Currency Exchange
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 22648   Accepted: 8180

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

题意是给出了M种钱币,N个转换所,每个钱币的转换关系,中间要给转换所交一定的中介费。求是否能够有这样的条件,使得钱在转了一圈之后变多了。

看到只需输入YES/NO这样,不用求具体的单源点最短路径或是所有的,就感觉很符合Bellman。。。还有一点,Bellman和Floyd能够处理负权值的图,但Flyod不能有负环。Dijsktra不能处理含有负权值的图。这题用Bellman只是在给每条边松弛的时候不再是简单的相加了,而是dis[edge[j].s]-edge[j].com)*edge[j].l 。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; struct E{
int s;
int e;
double com;
double l;
}edge[5205]; int N,M,S,edge_num;
double V;
double dis[505]; void addedge(int start,int end,double co,double len)
{
edge_num++; edge[edge_num].s=start;
edge[edge_num].e=end;
edge[edge_num].com=co;
edge[edge_num].l=len;
} bool bellman_ford()
{
int i,j;
for(i=1;i<=N-1;i++)
{
int flag=0;
for(j=1;j<=edge_num;j++)
{
if(dis[edge[j].e]<(dis[edge[j].s]-edge[j].com)*edge[j].l)
{
flag=1;
dis[edge[j].e]=(dis[edge[j].s]-edge[j].com)*edge[j].l;
}
}
if(flag==0)
break;
} for(j=1;j<=edge_num;j++)
{
if(dis[edge[j].e]<(dis[edge[j].s]-edge[j].com)*edge[j].l)
return true;
} return false;
} int main()
{
int i,start,end;
double co,len; edge_num=0;
memset(dis,0,sizeof(dis)); cin>>N>>M>>S>>V; for(i=1;i<=M;i++)
{
cin>>start>>end>>len>>co;
addedge(start,end,co,len); cin>>len>>co;
addedge(end,start,co,len);
}
dis[S]=V;
if(bellman_ford())
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
} return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1860:Currency Exchange的更多相关文章

  1. 【POJ 1860】Currency Exchange

    [题目链接]:http://poj.org/problem?id=1860 [题意] 给你n种货币,m种货币之间的交换信息; 交换信息以 A,B,RA,CA,RB,CB的形式给出; 即A换B的话假设A ...

  2. (poj)1806 Currency Exchange

    题目链接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our ...

  3. POJ 3903:Stock Exchange(裸LIS + 二分优化)

    http://poj.org/problem?id=3903 Stock Exchange Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  4. Currency Exchange POJ - 1860 (spfa)

    题目链接:Currency Exchange 题意: 钱的种类为N,M条命令,拥有种类为S这类钱的数目为V,命令为将a换成b,剩下的四个数为a对b的汇率和a换成b的税,b对a的汇率和b换成a的税,公式 ...

  5. 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19881   Accepted: 711 ...

  6. poj 1860 Currency Exchange :bellman-ford

    点击打开链接 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16635   Accept ...

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

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

  8. (最短路 SPFA)Currency Exchange -- poj -- 1860

    链接: http://poj.org/problem?id=1860 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2326 ...

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

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

随机推荐

  1. 吴裕雄--天生自然java开发常用类库学习笔记:foreach及Enumeration接口

    import java.util.Vector; import java.util.Enumeration; public class EnumerationDemo01{ public static ...

  2. 【LeetCode】226. 翻转二叉树

    题目 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 本题同[剑指Offer]面试题27. 二叉树的镜 ...

  3. hihocoder 1241:Best Route in a Grid

    #1241 : Best Route in a Grid 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个N行N列的非负整数方阵,从左上角(1,1)出发,只能向下 ...

  4. MYSQL 免安装版修改root密码

    c:>mysql –uroot; mysql>show databases; mysql>use mysql;//不用修改 mysql>UPDATE user SET pass ...

  5. TX2开发板Ubuntu16.04设置静态IP

    TX2开发板Ubuntu16.04设置静态IP  https://www.cnblogs.com/qilai/p/11285445.html 首先打开一个Terminal输入 ifconfig 查看自 ...

  6. asp.net增加指定404页面

    对于在asp中添加404页面我并不熟悉,所以刚开始只能在网上找各种资料,网上资源太多,各种借鉴.   1.借鉴网上尝试的第一种方法:   首先,修改应用程序根目录的设置,打开 “web.config” ...

  7. 115-PHP实例化和不实例化输出类常量

    <?php class boy{ //定义男孩类 const sex='男'; public $age=15; } echo '不实例化对象输出类常量:'.boy::sex; $boy=new ...

  8. 103-PHP定义一个类

    <?php class ren{ //定义人类 } class mao{ //定义猫类 } new ren(); //实例化人类 new mao(); //实例化猫类 new mao(); // ...

  9. jQuery原理系列-工具函数

    jquery源码中有很多精妙的实现,对于我们每天都在使用的东西,一定要知其原理,如果遇到不能使用jquery环境,也能自己封装原生的代码实现. 1.检测类型 众所周知typeof 不能用来检测数据,会 ...

  10. Linux每日一练20200219