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.
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.
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 题意:要知道,每两种货币的兑换都是不同的,货币A-B和货币B-A也是不同的,所以经过一系列的货币兑换后,本来持有的钱会出现增加后减少的情况,那么现在知道货币现在的价值,种类,以及有多少种货币和转化方式,求经过一系列的兑换后回到最初的种类,价值是否增加(那么最短路中回路问题就出现了)。
#include<stdio.h> #include<queue> #include<algorithm> using namespace std; const int N=110; const int INF=0x3f3f3f3f; double C[N][N], R[N][N], dist[N], V; ///C数组保存货币兑换的利率,R数组保存货币兑换需要缴纳的值 int vis[N], cou[N], n, s; ///cou数组保存每个点遍历的次数,如果次数>=n的话说明出现回路了,代表价值值增加 void Init() { int i, j; for (i = 1; i <= n; i++) { dist[i] = -INF; cou[i] = vis[i] = 0; for (j = 1; j <= n; j++) C[i][j] = R[i][j] = -INF; C[i][i] = R[i][i] = 0; } } int Spfa(int u) { int i, v; queue<int>Q; Q.push(u); dist[u] = V; cou[u]++; while (!Q.empty()) { v = Q.front(); Q.pop(); for (i = 1; i <= n; i++) { if (dist[i] < (dist[v]-C[v][i])*R[v][i]) { dist[i] = (dist[v]-C[v][i])*R[v][i]; if (!vis[i]) { Q.push(i); vis[i] = 1; cou[i]++; if (cou[i] >= n) return 1; ///判断是否有回路,只需要判断是否存在回路的原因是dist[i]只有变大了才会进行这一步,即价值增加 } } } vis[v] = 0; } if (dist[s] > V) return 1; ///没有回路但是价值增加了也是可行的(感觉这一步像废话,如果增加了肯定是有回路存在啊!) return 0; } int main () { int m, a, b, ans; double r1, r2, c1, c2; while (scanf("%d %d %d %lf", &n, &m, &s, &V) != EOF) { Init(); while (m--) { scanf("%d %d %lf %lf %lf %lf", &a, &b, &r1, &c1, &r2, &c2); C[a][b] = c1; C[b][a] = c2; R[a][b] = r1; R[b][a] = r2; } ans = Spfa(s); if (ans) printf("YES\n"); else printf("NO\n"); } return 0; }