Arbitrage(POJ-2240 && HDU-1217)
2017-05-24 10:10
519 查看
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
Sample Output
题意:给出一些货币和货币之间的兑换比率,问是否可以使某种货币经过一些列兑换之后,货币值增加。举例说就是1美元经过一些兑换之后,超过1美元。可以输出Yes,否则输出No。
思路:弗洛伊德算法,模板题;
ps:居然在输出上错了那么多次,tm把Yes和No写成YES和NO。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#define maxn 105
using namespace std;
double a[maxn][maxn];
int m,n;
void Init()
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(i==j) a[i][j]=1.0;
else a[i][j]=0.0;
}
}
}
void Floyd()
{
for(int k=1; k<=n; k++)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(a[i][j]<a[i][k]*a[k][j])
a[i][j]=a[i][k]*a[k][j];
}
}
}
}
int main()
{
int t=0;
char s[maxn],str1[maxn],str2[maxn];
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
map<string,int>M;
t++;
Init();
for(int i=1; i<=n; i++)
{
scanf("%s",s);
M[s]=i;
}
scanf("%d",&m);
for(int i=0; i<m; i++)
{
int from,to;
double cost;
scanf("%s%lf%s",str1, &cost, &str2);
from=M[str1];
to=M[str2];
a[from][to]=cost;
}
Floyd();
bool flag=false;
for(int i=1; i<=n; i++)
{
if(a[i][i]>1.0)
{
flag=true;
break;
}
}
printf("Case %d: ",t);
if(flag)
{
printf("Yes\n");
}
else printf("No\n");
}
return 0;
}
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 0
Sample Output
Case 1: Yes Case 2: No
题意:给出一些货币和货币之间的兑换比率,问是否可以使某种货币经过一些列兑换之后,货币值增加。举例说就是1美元经过一些兑换之后,超过1美元。可以输出Yes,否则输出No。
思路:弗洛伊德算法,模板题;
ps:居然在输出上错了那么多次,tm把Yes和No写成YES和NO。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#define maxn 105
using namespace std;
double a[maxn][maxn];
int m,n;
void Init()
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(i==j) a[i][j]=1.0;
else a[i][j]=0.0;
}
}
}
void Floyd()
{
for(int k=1; k<=n; k++)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(a[i][j]<a[i][k]*a[k][j])
a[i][j]=a[i][k]*a[k][j];
}
}
}
}
int main()
{
int t=0;
char s[maxn],str1[maxn],str2[maxn];
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
map<string,int>M;
t++;
Init();
for(int i=1; i<=n; i++)
{
scanf("%s",s);
M[s]=i;
}
scanf("%d",&m);
for(int i=0; i<m; i++)
{
int from,to;
double cost;
scanf("%s%lf%s",str1, &cost, &str2);
from=M[str1];
to=M[str2];
a[from][to]=cost;
}
Floyd();
bool flag=false;
for(int i=1; i<=n; i++)
{
if(a[i][i]>1.0)
{
flag=true;
break;
}
}
printf("Case %d: ",t);
if(flag)
{
printf("Yes\n");
}
else printf("No\n");
}
return 0;
}
相关文章推荐
- POJ 2240 && HDU 1217 Arbitrage(Floyd)
- hdu 1217/poj 2240 Arbitrage【floyd】
- POJ 2240 HDU 1217 Arbitrage
- POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)
- zoj 1092 && poj 2240 Arbitrage (floyd算法)
- POJ 2240 or HDU 1217 Arbitrage 【spfa判环】
- HDU 1217 and POJ 2240 Arbitrage
- POJ - 2240 Arbitrage (最短路 Floyd算法 && Bellman-Ford算法)
- POJ 2240 - Arbitrage(bellman_ford & floyd)
- 【map && spfa求正权回路】HDU - 1217 Arbitrage
- poj 2240 Arbitrage 【最短路&&floyd】
- POJ 2240 && ZOJ 1082 Arbitrage 最短路,c++ stl pass g++ tle 难度:0
- POJ2240-Arbitrage-C语言&&NYOJ-188
- HDU 1217 Arbitrage 解题报告
- poj 1151 && hdu 1542 求矩形面积并(线段树)
- POJ 2240, Arbitrage
- hdu 1217 Arbitrage--Floyd
- poj 1177 && hdu 1828
- poj 2240 Arbitrage
- HDU_1051 && POJ_1065 Wooden Sticks(贪心)