本文主要是介绍POJ1860 Currency Exchange(正权环,folyd),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
Currency Exchange
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 BAand 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 Source Northeastern Europe 2001, Northern Subregion |
[Submit] [Go Back] [Status] [Discuss]
题目大意:有N种货币,货币之间可以按汇率交换,同时还需要收手续费,当你用100A货币去交换B货币,
假如A到B的汇率为29.75,手续费为0.39,则你可以得到(100-0.39)*29.75 = 2963.3975的B货币。货币
可以一直重复交换,问:能否通过兑换货币之后,增加你手中货币的价值,则输出"YES",否则输出"NO"。
思路:把N种货币看成图上的N个点,当你有数量为V的货币A时,
货币AB之间的权值就是——(V-手续费)*A到B的汇率
这道题就可以转换为求图是否还有可无限增大(含有正权回路)的最大路径。
思路:
这个题最终判定是否存在一个无限增大的正权环,先用folyd跑一次,然后再来一次的时候发现还可以继续松弛,那就证明存在,详细的看注释。
代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define N 100+20
#define M 100000+20
#define inf 0x3f3f3f3f
using namespace std;
int n,m,s;
double v,map[N]= {0},g1[N][N]= {0},g2[N][N]= {0};
int floyd()
{double dis[N];for(int i=1; i<=n; i++)dis[i]=map[i];for(int k=1; k<=n; k++)for(int i=1; i<=n; i++)for(int j=1; j<=n; j++)if((map[i]-g2[i][j])*g1[i][j]>map[j])map[j]=(map[i]-g2[i][j])*g1[i][j];for(int i=1; i<=n; i++)if(dis[i]<map[i])//当进行第二次floyd的时候dis里面的信息是已经更新一次的map里的值,如果还能继续更新那就证明有正权环return 1;return 0;
}
int main()
{while(cin>>n>>m>>s>>v){for(int i=1; i<=m; i++){int a,b;double h_ab,m_ab,h_ba,m_ba;cin>>a>>b>>h_ab>>m_ab>>h_ba>>m_ba;//h_ab代表汇率从a->b,m_ab表示佣金从a->bg1[a][b]=h_ab,g2[a][b]=m_ab;g1[b][a]=h_ba,g2[b][a]=m_ba;}map[s]=v;floyd();if(floyd())cout<<"YES"<<endl;elsecout<<"NO"<<endl;}return 0;
}
这篇关于POJ1860 Currency Exchange(正权环,folyd)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!