POJ 2240-Arbitrage

POJ 2240-Arbitrage Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16195 Accepted: 6814 Description Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent. Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not. Input The input will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible. Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n. Output For each test case, print one line telling whether arbitrage is possible or not in the format “Case case: Yes” respectively “Case case: No”. Sample Input 3 USDollar BritishPound FrenchFranc 3 USDollar 0.5 BritishPound BritishPound 10.0 FrenchFranc FrenchFranc 0.21 USDollar 3 USDollar BritishPound FrenchFranc 6 USDollar 0.5 BritishPound USDollar 4.9 FrenchFranc BritishPound 10.0 FrenchFranc BritishPound 1.99 USDollar FrenchFranc 0.09 BritishPound FrenchFranc 0.19 USDollar Sample Output Case 1: Yes Case 2: No Source Ulm Local 1996


本题的题意是“通过汇率的转换,有没有可能获利”。题目给出了不同货币之间的汇率,这其实就给出了一张图,我们需要从图中找出一条回路,回路上所有汇率的乘积大于1,则可以获利;如果所有回路的乘积都小于等于1,则不可能获利。 仔细想想,这有点像求所有点对的最短距离!只是这题的最短距离实际上是最大乘积值,以往我们求最短距离的时候,图中点A到自身的距离是0;但是本题A到A自身的距离(汇率)应该是1。利用Floyd算法求解之后,如果存在path[A][A]>1则可获利,否则不可获利。 所以总的来说,这题还是不难的。需要注意的是,输入的时候给出的是字符串,我们需要把它转换成整型对应关系,可以利用map<string,int>来保存string和int的对应关系,方便后面的Floyd算法实施。 完整代码如下: [cpp] #include<iostream> #include<string> #include<map> using namespace std; const int MAX_N=31; double path[MAX_N][MAX_N]; //初始化,A到A的路径(汇率)为1;其他为0 void init_path(int n) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(i==j) path[i][j]=1.0; else path[i][j]=0.0; } } } //Floyd算法求所有点对的“最短距离”(最大乘积值) void Floyd(int n) { for(int k=0;k<n;k++) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { path[i][j]=(path[i][j]>path[i][k]*path[k][j])?path[i][j]:(path[i][k]*path[k][j]); } } } } int main() { //freopen("input.txt","r",stdin); int n,m; int case_num=1; while(cin>>n&&n) { init_path(n); map<string,int> currency_index;//保存string和int的对应关系 int i=0; string tmp; for(int j=0;j<n;j++) { cin>>tmp; currency_index[tmp]=i++; } cin>>m; string ci,cj; double rij; while(m–) { cin>>ci>>rij>>cj; path[currency_index[ci]][currency_index[cj]]=rij; } Floyd(n); bool yes=false; for(i=0;i<n;i++) { if(path[i][i]>1) { yes=true; break; } } if(yes) cout<<"Case "<<case_num<<": Yes"<<endl; else cout<<"Case "<<case_num<<": No"<<endl; case_num++; } return 0; } [/cpp] 本代码提交AC,用时94MS,内存236K。]]>

1 thought on “POJ 2240-Arbitrage

  1. Pingback: POJ 2263-Heavy Cargo | bitJoy > code

Leave a Reply

Your email address will not be published. Required fields are marked *