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.
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.
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
题意:
第一行输入n,m,s,money分别代表有n种钱币种类(样例中为1,2,3),m个货币兑换点,你拥有的货币种类,money为你的钱币数(注意不是int类型)
以下m行为u,v,r1,c1,r2,c2为u换取v钱币的兑换率为r1,佣金为c1,v换取u钱币的兑换率为r2,佣金为c2;
思路:找出是否存在正权回路(正权回路:在这一回路上,顶点的权值能不断增加即能一直进行松弛)一种货币就是图上的一个点,一个兑换点就是两个货币之间的一个兑换环权值为(钱数-佣金)*兑换率。
ps:原谅我个渣比好几天才理解题意
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define inf 99999999
struct node
{
int u,v;
double r,c;
}edge[220];
int n,m,s;
double money;
double dis[110];//保存最短路径
int cnt;
void add_edge(int uu,int vv,double rr,double cc)//加边操作
{
edge[cnt].u=uu;
edge[cnt].v=vv;
edge[cnt].r=rr;
edge[cnt].c=cc;
cnt++;
}
int bellman_ford()
{
int i,j;
for(i=1;i<=n;i++)
dis[i]=0;
dis[s]=money;
for(i=1;i<=n;i++)
{
int flag=0;
for(j=0;j<cnt;j++)//对每个边
{
if(dis[edge[j].v]<(dis[edge[j].u]-edge[j].c)*edge[j].r)//进行松弛操作
{
dis[edge[j].v]=(dis[edge[j].u]-edge[j].c)*edge[j].r;
flag=1;//松弛操作成功标记为1;
}
}
if(!flag)//若所有的边i有没松弛成功的就退出
break;
}
for(i=0;i<cnt;i++)
{
if(dis[edge[i].v]<(dis[edge[i].u]-edge[i].c)*edge[i].r)//进行|V|-1次操作后 有边还能进行松弛 说明
return 1;//存在正权回路
}
return 0;//不存在正权回路
}
int main()
{
int u,v;
double r1,c1,r2,c2;
while(~scanf("%d %d %d %lf",&n,&m,&s,&money))
{
cnt=0;
while(m--)
{
scanf("%d %d %lf %lf %lf %lf",&u,&v,&r1,&c1,&r2,&c2);
add_edge(u,v,r1,c1);
add_edge(v,u,r2,c2);
}
if(bellman_ford())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}