Arbitrage
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6328 Accepted Submission(s): 2925
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.
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.
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
Source
University of Ulm Local Contest 1996
题目意思:钱币之间的兑换,最后如果赚了就yes,赚不了就no
SPFA
#include <iostream>
#include <queue>
#include <map>
#include <string>
using namespace std;
#define maxint 0x3f3f3f3f
#define maxnum 35
double c[maxnum][maxnum];//输入两个节点以及之间的权值
double dist[maxnum];
char str[35],s1[35],s2[35];
map<string,int> mp;
int n;
int spfa(int nn)
{
int vis[maxnum];
int i,u;
for(i=1;i<=n;i++)
{
dist[i] = 0;
vis[i] = 0;
}
queue<int> q;
while(!q.empty())
q.pop();
dist[nn] = 1.0;
vis[nn] = 1;
q.push(nn);
while(!q.empty())
{
u = q.front();
q.pop();
vis[u] = 0;
for(i=1;i<=n;i++)
{
if(dist[u] * c[u][i] > dist[i])
{
dist[i] = dist[u] * c[u][i];
if(dist[nn] > 1.0)
return 1;
if(!vis[i])
{
vis[i] = 1;
q.push(i);
}
}
}
}
return 0;
}
int main()
{
int i,j,k;
int line;
int p,q;
k=0;
while(cin>>n,n)
{
k++;
for(i=1;i<=n;i++)//初始化
for(j=1;j<=n;j++)
c[i][j] = i==j?1.0:0;
for(i=1;i<=n;i++)
{
scanf("%s",str);
mp[str] = i;
}
scanf("%d",&line);
double rate;
for(i=1;i<=line;i++)
{
scanf("%s%lf%s",s1,&rate,s2);
c[mp[s1]][mp[s2]] = rate;
}
int flag = 0;
for(i=1;i<=n;i++)
{
if(spfa(i))
{
flag = 1;
break;
}
}
if(flag)
printf("Case %d: Yes\n",k);
else
printf("Case %d: No\n",k);
}
return 0;
}
BELLMAN-FORD
#include <iostream>
#include <queue>
#include <map>
#include <string>
using namespace std;
#define maxint 0x3f3f3f3f
#define maxnum 35
double c[maxnum][maxnum];//输入两个节点以及之间的权值
double dist[maxnum];
char str[35],s1[35],s2[35];
map<string,int> mp;
int bellman_ford(int n)
{
int vis[maxnum];
int i,j,u;
for(i=1;i<=n;i++)
{
dist[i] = 0;
vis[i] = 0;
}
dist[1] = 1;
for(int k=1;k<n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(dist[j] < dist[i]*c[i][j])
dist[j] = dist[i]*c[i][j];
}
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(dist[j] < dist[i]*c[i][j])
return 1;
}
}
return 0;
}
int main()
{
int i,j,k,n;
int line;
int p,q;
k=0;
while(cin>>n,n)
{
k++;
for(i=1;i<=n;i++)//初始化
for(j=1;j<=n;j++)
c[i][j] = i==j?1.0:0;
for(i=1;i<=n;i++)
{
scanf("%s",str);
mp[str] = i;
}
scanf("%d",&line);
double rate;
for(i=1;i<=line;i++)
{
scanf("%s%lf%s",s1,&rate,s2);
c[mp[s1]][mp[s2]] = rate;
}
if(bellman_ford(n))
{
printf("Case %d: Yes\n",k);
}
else
printf("Case %d: No\n",k);
}
return 0;
}
Floyd
#include <iostream>
#include <queue>
#include <map>
#include <string>
using namespace std;
#define maxint 0x3f3f3f3f
#define maxnum 35
double c[maxnum][maxnum];//输入两个节点以及之间的权值
double dist[maxnum];
char str[35],s1[35],s2[35];
map<string,int> mp;
int floyd(int n)
{
int i,j;
for(int k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(c[i][j] < c[i][k]*c[k][j])
c[i][j] = c[i][k]*c[k][j];
}
}
}
for(i=1;i<=n;i++)
{
if(c[i][i] > 1)
return 1;
}
return 0;
}
int main()
{
int i,j,k,n;
int line;
int p,q;
k=0;
while(cin>>n,n)
{
k++;
for(i=1;i<=n;i++)//初始化
for(j=1;j<=n;j++)
c[i][j] = i==j?1.0:0;
for(i=1;i<=n;i++)
{
scanf("%s",str);
mp[str] = i;
}
scanf("%d",&line);
double rate;
for(i=1;i<=line;i++)
{
scanf("%s%lf%s",s1,&rate,s2);
c[mp[s1]][mp[s2]] = rate;
}
if(floyd(n))
{
printf("Case %d: Yes\n",k);
}
else
printf("Case %d: No\n",k);
}
return 0;
}