Currency Exchange bellman_ford算法,找正环,

Problem 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
*****************************************************************************************************************************看代码
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<cstdio>
 6 #include<queue>
 7 using namespace std;
 8 double dis[10001];
 9 int n,m,u,v;
10 double money,r1,c1,r2,c2;
11 struct edge
12 {
13     int u,v;
14     double r,c;
15 }e[1000001];
16 int i,j,k,s;
17 int bellman_ford()
18 {
19     //cout<<"M: "<<m<<endl;
20     for(int it=0;it<=n;it++)
21       dis[it]=0.0;
22     dis[s]=money;
23     for(int it=1;it<n;it++)//松弛n-1次
24     {
25         int flag=1;
26         //cout<<"dis:: "<<dis[it]<<endl;
27         for(int jt=0;jt<m;jt++)
28         {
29             int u=e[jt].u;
30             int v=e[jt].v;
31             double r=e[jt].r;
32             double c=e[jt].c;
33 
34           if(dis[v]<(dis[u]-c)*r)
35           {
36             dis[v]=(dis[u]-c)*r;
37             flag=0;
38           }
39           //cout<<"dis["<<v<<"]: "<<dis[v]<<endl;
40         }
41         if(flag==1)  return false;//找不到要松弛的提前返回
42 
43     }
44     for(int it=0;it<m;it++)//找是否存在正环
45       if(dis[e[it].v]<(dis[e[it].u]-e[it].c)*e[it].r)
46         return true;
47     return false;
48 }
49 int main()
50 {
51     while(scanf("%d%d%d%lf",&n,&m,&s,&money)!=EOF)
52     {
53         k=0;
54         for(i=1;i<=m;i++)
55         {
56             //双向存储
57             scanf("%d%d%lf%lf%lf%lf",&u,&v,&r1,&c1,&r2,&c2);
58             e[k].u=u;
59             e[k].v=v;
60             e[k].r=r1;
61             e[k++].c=c1;
62             e[k].u=v;
63             e[k].v=u;
64             e[k].r=r2;
65             e[k++].c=c2;
66         }
67         m=2*m;//边的二倍;
68         if(bellman_ford())
69             printf("YES\n");
70         else
71             printf("NO\n");
72     }
73     return 0;
74 }
View Code

posted @ 2013-10-19 16:28  persistent codeants  阅读(260)  评论(0编辑  收藏  举报