18.11.16 POJ 1860 Currency Exchange(有向图最短路)

本文探讨了在一个存在多种货币及兑换点的城市中,通过特定的货币兑换策略,是否有可能实现资本增值的问题。通过构建图论模型,使用Bellman-Ford算法检测是否存在负权重循环,从而判断是否有机会通过一系列兑换操作增加初始资金。
摘要由CSDN通过智能技术生成

描述

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<=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.
输出

If Nick can increase his wealth, output YES, in other case output NO to the output file.

样例输入

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

样例输出

YES

来源

Northeastern Europe 2001, Northern Subregion

 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <vector>
11 #include <fstream>
12 #include <set>
13 #define  inf 999999;
14 
15 using namespace std;
16 int n, m, s;
17 double v;
18 double dist[101];
19 struct edge{
20     int from, to;
21     double rate,comm;
22     edge() {}
23     edge(int a,int b,double c,double d) {
24         from = a, to = b;
25         rate = c, comm = d;
26     }
27 };
28 vector<edge>all;
29 
30 void bellmanford(){
31     for (int i = 1; i < n; i++)
32     {
33         bool flag = false;
34         for (int i = 0; i < all.size(); i++) {
35             int s = all[i].from, e = all[i].to;
36             double newval = (dist[s] - all[i].comm)*all[i].rate;
37             if (dist[e] < newval)
38             {
39                 flag = true;
40                 dist[e] = newval;
41             }
42         }
43         if (!flag)
44             break;
45     }
46     for (int i = 0; i < all.size(); i++) {
47         int s = all[i].from, e = all[i].to;
48         double newval = (dist[s] - all[i].comm)*all[i].rate;
49         if (dist[e] < newval)
50         {
51             printf("YES\n");
52             return;
53         }
54     }
55     printf("NO\n");
56 }
57 
58 void init() {
59     scanf("%d%d%d%lf", &n, &m, &s, &v);
60     for (int i = 1; i <= m; i++) {
61         int x, y;
62         double r1, r2, c1, c2;
63         scanf("%d%d", &x, &y);
64         scanf("%lf%lf%lf%lf", &r1, &c1, &r2, &c2);
65         all.push_back(edge(x, y, r1,c1));
66         all.push_back(edge(y, x, r2,c2));
67     }
68     dist[s] = v;
69     bellmanford();
70 }
71 
72 int main()
73 {
74     init();
75     return 0;
76 }
View Code

wa点:

现在拿的币种不一定是1号!

 

 1 int n, m, s;
 2 double v;
 3 struct Edge {
 4     int s, e;
 5     double rse, cse,res,ces;
 6 }edge[maxn];
 7 double money[maxn];
 8 
 9 void init() {
10     scanf("%d%d%d%lf", &n, &m, &s, &v);
11     for (int i = 1; i <= m; i++)
12         scanf("%d%d%lf%lf%lf%lf", &edge[i].s, &edge[i].e,
13             &edge[i].rse, &edge[i].cse, &edge[i].res, &edge[i].ces);
14     money[s] = v;
15     for (int k = 1; k <= 2*m; k++)
16     {
17         for (int i = 1; i <= m; i++) {
18             int x = edge[i].s, y = edge[i].e;
19             double se = (money[x] - edge[i].cse)*edge[i].rse;
20             double es = (money[y] - edge[i].ces)*edge[i].res;
21             if (money[x] && se > money[y])
22                 money[y] = se;
23             if (money[y] && es > money[x])
24                 money[x] = es;
25         }
26     }
27     for (int i = 1; i <= m; i++) {
28         int x = edge[i].s, y = edge[i].e;
29         double se = (money[x] - edge[i].cse)*edge[i].rse;
30         double es = (money[y] - edge[i].ces)*edge[i].res;
31         if (money[x] && se > money[y]) {
32             printf("YES\n");
33             return;
34         }
35         if (money[y] && es > money[x]) {
36             printf("YES\n");
37             return;
38         }
39     }
40     printf("NO\n");
41 }
View Code

 

更新!

 

转载于:https://www.cnblogs.com/yalphait/p/9970929.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值