POJ 1860-Currency Exchange(最短路-回路套汇)

来源:互联网 发布:淘宝怎么买汽车票报销 编辑:程序博客网 时间:2024/06/15 17:07

Currency Exchange
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 28315 Accepted: 10548

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.01 2 1.00 1.00 1.00 1.002 3 1.10 1.00 1.10 1.00

Sample Output

YES

Source

Northeastern Europe 2001, Northern Subregion

题目意思:

一些货币兑换点正在我们的城市工作。我们假设每个兑换点尤其擅长兑换两种特定的货币,并且只进行对这些货币的兑换。可能有一些兑换点专门兑换相同的货币对。每个兑换点都有自己的汇率,货币A到货币B的汇率是你用1货币A换到的货币B的数量。每个兑换点也有一些佣金,即为你需要为你的兑换行动支付的金额。佣金总是从源货币扣除。例如,如果你想要在汇率为29.75,并且佣金为0.39的兑换点将100美元兑换成俄罗斯卢布,你将会得到(100-0.39)*29.75=2963.3975卢布。你有N种不同的货币可以在我们的城市进行兑换。我们为每一种货币制定从1到N的唯一一个整数。每个兑换点可以用6个数字形容:整数A和B——它交换的货币(表示为货币A和货币B);实数RAB,CAB,RBA和CBA——当它分别将货币A兑换成货币B和将货币B兑换成货币A时的汇率和佣金。Nick有一定数量的货币S,并且它希望它能以某种方式在一些兑换行动后增加它的资本。最后它的资金必须兑换为货币S。帮助他解决这个棘手的问题。叫兽在进行它的兑换行动时资金总数必须始终非负。Input 输入的第一行包含四个数字:N – 货币的数量,M – 兑换点的数量,S – Nick具有的货币种类,V – Nick具有的货币数量。下面的M行每行包括6个数字 – 对相应的兑换点的描述。数字相隔一个或多个空格。1<=S<=N<=100,1<=M<=100,V是实数,0<=V<=10^3。对于每个兑换点汇率和佣金都是实数,小数点后最多有两位小数。10^-2<=汇率<=10^2,0<=佣金<=10^2。如果在一组兑换行动中没有兑换点被超过一次地使用,我们认为这组兑换行动是简单的。你可以认为最终数值和最初任何一个简单的兑换行动组的比例小于10^4。Output 如果Nick能增加它的财产,输出YES,否则输出NO。

解题思路:

将不同种类的货币编号,汇率和佣金计算后的资本是路径权值,建立有向图。

从Nick拥有的货币S节点出发,数量V经过回路返回S,是否大于原值S。

若大于或存在正权回路则说明可以套汇。


YES写成Yes然后WA了一晚上不明所以,真是被自己蠢哭了,可能我以前AC的都是假题……Orz


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <queue>#include <map>#include <algorithm>using namespace std;#define INF 0xfffffff#define MAXN 1010struct Edge{    int u,v;    double r,c;//汇率和佣金};Edge edge[MAXN];//邻接表int n,m,s;//顶点数、边数、拥有的货币编号double v;//拥有的货币数量double dist[MAXN];//顶点s到其他顶点的最长路径bool Bellman(int s)//顶点s到其他顶点的最长路径{    int i,j;    for(i=0; i<=n; i++) dist[i]=0;    dist[s]=v;    for(i=0; i<n; ++i)        for(j=0; j<m; ++j)        {            Edge e=edge[j];            double t=e.r*(dist[e.u]-e.c);            if(t>dist[e.v])//判断是否增加了最大距离                dist[e.v]=t;            if(dist[s]>v)//可以套汇                return true;        }    for(i=0; i<m; i++)        if(dist[edge[i].v]<(dist[edge[i].u]-edge[i].c)*edge[i].r)            return true;    return false;}int main(){#ifdef ONLINE_JUDGE#else    freopen("F:/cb/read.txt","r",stdin);    //freopen("F:/cb/out.txt","w",stdout);#endif    ios::sync_with_stdio(false);    cin.tie(0);    while(cin>>n>>m>>s>>v)    {        int i,cnt=0;        for(i=0; i<m; ++i)        {            cin>>edge[cnt].u>>edge[cnt].v>>edge[cnt].r>>edge[cnt].c;//读入汇率            ++cnt;            edge[cnt].v=edge[cnt-1].u;            edge[cnt].u=edge[cnt-1].v;            cin>>edge[cnt].r>>edge[cnt].c;            ++cnt;        }    }    m*=2;    if(Bellman(s)) cout<<"YES"<<endl;//顶点s到其他顶点    else cout<<"NO"<<endl;    return 0;}


0 0