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.00Sample Output
YESSource
//题意:给你N个币种,M中币种交换方式,s表示你的初始币种,v表示你手头上s币种有多少;然后输入M种交换方式, //每种有6个输入分别是a, b表示a,b两种币种间可以交换,然后rab,cab表示a交换到b的利率和手续费,最后rba,rbc //表示b交换到a的利率和手续费;现在问你通过不断的在各个币种之间交换,初始的v单位的s币种,能否在不断交换后回到s币种时,能否比v单位要多(就是转一圈后,回到出发点发现自己赚钱了!!) //理解:以货币种类为点,以交换方式为边,我们来构交换方式的图G(G的表示看G的结构体); #include<iostream> #include<cstdio> #include<queue> #include<algorithm> #include<cstring> #include<vector> #include<cmath> using namespace std; const int maxn = 110; struct node{ int a, b; //a原交换点;b目标交换点 double r, c; //r是利率, c表示commission(手续费); }G[2*maxn+5]; int n, m, s; //n个点,m条边,s表示出发点, double v; //v表示当前s这种货币咱手头上有多少; double dis[maxn]; //dis[i]表示当前币种i(在不进行图中圈循环的条件下!!求最大路后)v单位s币种转换到i币种后最大得到的钱数; bool bellom_ford(int M){ memset(dis, 0, sizeof(dis)); dis[s] = v; for(int i = 1; i <= n-1; i++){ bool update = false; for(int k = 0; k < M; k++){ node e = G[k]; if(dis[e.b] < (dis[e.a]-e.c)*e.r){ //这一步就与最短路不同,因为我们这里求得是最大不是最小; dis[e.b] = (dis[e.a]-e.c)*e.r; update = true; } } if(!update) break; } for(int k = 0; k < M; k++){ //这里可能不好理解,我尽量讲明白; node e = G[k]; //我取出第k个交换方式(边),如果我经过上面完整的算法求出的每个币种dis,这个dis本应给对于每个币种来说是最大的 if(dis[e.b] < (dis[e.a] - e.c)*e.r) //但是现在我通过判断发现它还不是最大的,还可以变大,这样就存在一个递增的圈,我只要沿着这个圈一直搞,就可以 return true; //越赚越多。 } return false; } int main(){ int a, b; double rab, cab, rba, cba; while(~scanf("%d%d%d%lf", &n, &m, &s, &v)){ int k = 0; for(int i = 0; i < m; i++){ scanf("%d%d%lf%lf%lf%lf", &a, &b, &rab, &cab, &rba, &cba); G[k].a = a; G[k].b = b; G[k].r = rab; G[k].c = cab; k++; //两种不同的边分别存; G[k].a = b; G[k].b = a; G[k].r = rba; G[k].c = cba; k++; } bool flag = bellom_ford(k); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }