博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1860——Currency Exchange(Eellman-Ford+权值为正的环路)
阅读量:2345 次
发布时间:2019-05-10

本文共 3196 字,大约阅读时间需要 10 分钟。

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

一些价值为s的货币经过一系列的交换,是否能增加价值。抽象出来就是一种货币代表一个节点,节点之间的权值就是交换规则后的价值,因为最后是求最初的货币,因此是一个回路,货币经过这个回路就回升值,说明这个回路是正值的。

运用bellman-ford算法,原始的是判断是否存在负值的环路并求出最短路,这里只要稍微改下松弛操作就可以判断是否存在正值的环路

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 105#define mod 1000000007using namespace std;int n,m,s,e;double v,dis[MAXN];struct Node{ int beg,end; double r,c;};Node edge[MAXN<<1];void add(int b,int en,double r,double c){ edge[e].beg=b; edge[e].end=en; edge[e].r=r; edge[e].c=c; e++;}bool relax(int p){ double t=(dis[edge[p].beg]-edge[p].c)*edge[p].r; if(dis[edge[p].end]
v) return true; if(!flag) return false; } for(int j=0; j

转载地址:http://dscvb.baihongyu.com/

你可能感兴趣的文章
对类成员访问权限的控制,是通过设置成员的访问控制属性实现的,下列不是访问控制属性的是( )
查看>>
字符串常量放在只读存储区
查看>>
#define 和 typedef 的区别
查看>>
不属于冯诺依曼体系结构必要组成部分是:
查看>>
有1000亿条记录,每条记录由url,ip,时间组成,设计一个系统能够快速查询以下内容(程序设计题)
查看>>
最大堆---实现一个简化的搜索提示系统。给定一个包含了用户query的日志文件,对于输入的任意一个字符串s,输出以s为前缀的在日志中出现频率最高的前10条query。
查看>>
拓扑排序
查看>>
已知n阶矩阵A的行列式满足|A|=1,求|A^(-1)|(A^(-1)表示A的逆矩阵)=?
查看>>
已知一对夫妇有两个孩子,如果知道有一个是男孩,那么两个都是男孩的概率?
查看>>
视图包含下列结构是不可以更新的
查看>>
可能返回 null 的 SQL 语句
查看>>
以下关于STL的描述中,错误的有
查看>>
假设某棵二叉查找树的所有键均为1到10的整数,现在我们要查找5。下面____不可能是键的检查序列。
查看>>
给定一个整数sum,从有N个无序元素的数组中寻找元素a、b、c、d,使得 a+b+c+d =sum,最快的平均时间复杂度是____。
查看>>
设二叉树结点的先根序列、中根序列和后根序列中,所有叶子结点的先后顺序____。
查看>>
将整数序列(7-2-4-6-3-1-5)按所示顺序构建一棵二叉排序树a(亦称二叉搜索树),之后将整数8按照二叉排序树规则插入树a中,请问插入之后的树a中序遍历结果是____。
查看>>
IP地址、子网掩码、网络号、主机号、网络地址、主机地址
查看>>
已知int a[]={1,2,3,4,5};int*p[]={a,a+1,a+2,a+3};int **q=p;表达式*(p[0]+1)+**(q+2)的值是____。
查看>>
CPU输出数据的速度远远高于打印机的打印速度,为了解决这一矛盾,可采用()
查看>>
整型字符常量和字符字面量的区别 sizeof(char) 和 sizeof('a')
查看>>