有很多很多的货币交换站,可以把a b货币相互交换 ,交换站需要收取费用(单笔收取),问是否能最后能否将现有的货币经过交换之后越来越多(交换回原货币)
以货币为点,交换站为边(一个站两条边),dis初始值为0,bellman求最大路径,再求正回路
http://blog.csdn.net/lyy289065406/article/details/6645778 这里很清楚哦
Currency Exchange
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 14997 | Accepted: 5150 |
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 R AB, C AB, R BA and C BA - 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.
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 R AB, C AB, R BA and C BA - 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<=10
3.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10 -2<=rate<=10 2, 0<=commission<=10 2.
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 10 4.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10 -2<=rate<=10 2, 0<=commission<=10 2.
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 10 4.
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
//224K 16MS
#include <iostream>
using namespace std;int N,M,S,all_e;//N个点M条边,S初始货币种类,初始钱总数
double V;
// 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=103.
struct weight{
int s;
int e;
double rate,com;//10-2<=rate<=102, 0<=commission<=102.
}edge[220];
double dis[101];
bool bellman()
{
bool flag;
memset(dis,0,sizeof(dis) );//清零 距离最小
dis[S]=V;
/*relax*/
for(int i=0;i<N-1;i++)
{
flag=false;
for(int j=0;j<all_e;j++)
if(dis[edge[j].e] < ( dis[edge[j].s] - edge[j].com) * edge[j].rate )
{
dis[edge[j].e] = ( dis[edge[j].s] - edge[j].com) * edge[j].rate;
flag=true; //relax对路径有更新
}
if(!flag)
break; //只要某一次relax没有更新,说明最短路径已经查找完毕,或者部分点不可达,可以跳出relax
}
/*find posetive circle*/
for(int i=0;i<all_e;i++)
if( dis[edge[i].e] < (dis[edge[i].s]-edge[i].com) * edge[i].rate )
return 1;//找到正回路!成功!
return 0;//找不到正环
}
int main()
{
//0.定义与输入建图
scanf("%d %d %d %lf",&N,&M,&S,&V);
int n1,n2;
double r1,r2,c1,c2;
all_e= M * 2 ;
for(int i=0;i<all_e;)
{
scanf("%d %d %lf %lf %lf %lf",&n1,&n2,&r1,&c1,&r2,&c2);
edge[i].s = n1;
edge[i].e = n2;
edge[i].rate = r1;
edge[i].com = c1;
i++;
edge[i].s = n2;
edge[i].e = n1;
edge[i].rate = r2;
edge[i].com = c2;
i++;
}
if( bellman() )
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}