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 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
题目概述为你可以用一种钱兑换其他种类的钱,中间有汇率,如果你不断兑换最后兑换到你最初始的种类后你的钱变多了,那么你就是套现了。如果出现了这种套现的情况就输出Yes,否则No。该题和poj-1860为一同种思想,求最长路,并且在最后检查是否存在正环若存在则输出Yes。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
int x,y;
double w;
}arr[1500];
double dis[35];
int n,m;
void bellman()
{
int i,j;
for (i=1;i<=n;i++)
dis[i]=0;
dis[1]=1;
for (i=1;i<n;i++)
{
int flag=0;
for (j=1;j<=m;j++)
{
int x=arr[j].x,y=arr[j].y;
if(dis[y]<dis[x]*arr[j].w)
{dis[y]=dis[x]*arr[j].w;flag=1;}
}
if(!flag) break;
}
for (j=1;j<=m;j++)
{
int x=arr[j].x,y=arr[j].y;
if(dis[y]<dis[x]*arr[j].w)
{
printf("Yes\n");
break;
}
}
if(j==m+1) printf("No\n");
}
int main()
{
char str[35][30];
char str2[30];
double w;
int i,j,x,y,t=0;
while (scanf("%d%*c",&n),n)
{
t++;
for (i=1;i<=n;i++)
{
scanf("%s",str[i]);
}
scanf("%d",&m);
for (j=1;j<=m;j++)
{
scanf("%s",str2);
for (i=1;i<=n;i++)
{
if(strcmp(str2,str[i])==0)
{x=i;break;}
}
scanf("%lf%*c",&w);
scanf("%s",str2);
for (i=1;i<=n;i++)
{
if(strcmp(str2,str[i])==0)
{y=i;break;}
}
arr[j].x=x;
arr[j].y=y;
arr[j].w=w;
}
printf("Case %d: ",t);
bellman();
}
}