码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1860 货币兑换 SPFA判正环

时间:2014-09-10 22:27:51      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   strong   for   

Currency Exchange
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 20280   Accepted: 7270

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


题意:
总共n种货币(货币种类用1--n表示),m种货币之间的兑换(兑换是双向的)。Nick手中有s货币价值v。
货币之间兑换为a, b, rab, cab, rba, cba。 rab为a兑换b的汇率,cab为a兑换b所需要的手续费, rba和cba也是这样。 兑换b的时候 ,价值变化为(price[a]-cab)*rab。
题目问 Nick能否在货币间来回兑换使价值升高。

思路:
货币兑换若使价值升高必有正环,那么最长路判断正环就行了。


代码:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <queue>
 6 using namespace std;
 7 
 8 #define N 205
 9 
10 struct node{
11     int y;
12     double r, c;
13 };
14 
15 double dis[N];
16 int visited[N];
17 int num[N];
18 vector<node>ve[N];
19 int n, m, s;
20 double v;
21 
22 void init(){
23     int i;
24     for(i=0;i<=n;i++){
25         ve[i].clear();
26     }
27     memset(dis,0,sizeof(dis));
28     memset(visited,0,sizeof(visited));
29     memset(num,0,sizeof(num));
30 }
31 
32 int SPFA(){
33     
34     int i, j, u;
35     node p, q;
36     queue<int>Q;
37     Q.push(s);
38     dis[s]=v;
39     visited[s]=1;
40     while(!Q.empty()){
41         u=Q.front();
42         Q.pop();
43         visited[u]=0;
44         for(i=0;i<ve[u].size();i++){
45             p=ve[u][i];
46             if(dis[p.y]<(dis[u]-p.c)*p.r){
47                 dis[p.y]=(dis[u]-p.c)*p.r;
48                 if(!visited[p.y]){
49                     visited[p.y]=1;
50                     Q.push(p.y);
51                 }
52                 num[p.y]++;
53                 if(num[p.y]>=n) return 1;
54             }
55         }
56     }
57     return 0;
58 }
59 
60 main()
61 {
62     int i, j, k;
63     int x, y;
64     double r1, c1, r2, c2;
65     while(scanf("%d %d %d %lf",&n,&m,&s,&v)!=EOF){
66         init();
67         node p;
68         for(i=0;i<m;i++){
69             scanf("%d %d %lf %lf %lf %lf",&x,&y,&r1,&c1,&r2,&c2);
70             p.y=y;p.r=r1;p.c=c1;
71             ve[x].push_back(p);
72             p.y=x;p.r=r2;p.c=c2;
73             ve[y].push_back(p);
74         }
75         if(SPFA()) printf("YES\n");
76         else printf("NO\n");
77     }
78 }

 

POJ 1860 货币兑换 SPFA判正环

标签:des   style   blog   color   io   os   ar   strong   for   

原文地址:http://www.cnblogs.com/qq1012662902/p/3965251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!