Arbitrage
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7518 Accepted Submission(s): 3483
Problem 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.
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 file 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.
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 0
Sample Output
Case 1: Yes Case 2: No
杭电2000后又一道题我记得也是字符串 求最短路径 dijk...算法 这道题不太一样 因为可能有负权 而且变成了乘法 其实小于1就等同于加法里面的小于0 因此不能用dijk..算法
那咋办 用floyd呗 前面完全是模板 对于字符串要学会用map 非常的方便 。
其次 只要判断有无从自身的点回到自身的点的值大于1 有就是yes 没有就是no 这里可能比较难理解一点吧
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<map>
#include<algorithm>
using namespace std;
double mp[33][33],lv;
int i,j,k,l,m,n,x,y;
char c[100],s[100];
void floyd()
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(mp[j][k]<mp[j][i]*mp[i][k])
mp[j][k]=mp[j][i]*mp[i][k];
}
int main()
{
int kcase=1;
map<string,int> ma;
while(scanf("%d",&n),n){
int num=0;
memset(mp,0,sizeof(mp));
for(i=0;i<n;i++){
memset(c,0,sizeof(c));
scanf("%s",c);
ma[c]=num++;
}
scanf("%d",&m);
for(i=0;i<m;i++){
memset(c,0,sizeof(c));
memset(s,0,sizeof(s));
scanf("%s%lf%s",c,&lv,s);
mp[ma[c]][ma[s]]=lv;
}
floyd();
for(i=0;i<n;i++)
if(mp[i][i]>1) //这里就是判断从自身回到自身的时候 也就是出去换钱换了一圈回来能不能大于1 这下容易理解了吧
break;
if(i<n) printf("Case %d: Yes\n",kcase++);
else printf("Case %d: No\n",kcase++);
}
}