POJ 2240-Arbitrage(套匯-Bellman Ford)
Arbitrage
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20523 | Accepted: 8740 |
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
Source
題目意思:
套匯是利用匯率之間的差異,從而將一單位的某種貨幣,兌換回多於一單位的同種貨幣。
給出N種貨幣及其之間的匯率,判斷是否能夠進行套匯。
解題思路:
使用Bellman Ford,將最短路轉換成最長邊,依次從各個頂點出發,判斷是否存在大於1的情況(可以套匯)。
Note:因為我用了string陣列,所以G++可AC,C++會CE。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
#define INF 0xfffffff
#define MAXN 1010
struct Edge
{
int u,v;
double w;//匯率
};
Edge edge[MAXN];//鄰接表
int n,m;//頂點數和邊數
double dist[MAXN];//頂點s到其他頂點的最長路徑
bool flag=false;
void Bellman(int s)//頂點s到其他頂點的最長路徑
{
int i,j;
memset(dist,0,sizeof(dist));
dist[s]=1.0;
for(i=1; i<=n; ++i)
for(j=0; j<m; ++j)
{
Edge e=edge[j];
if(e.w*dist[e.u]>dist[e.v])//判斷是否增加了最大距離
{
dist[e.v]=e.w*dist[e.u];
//cout<<"dist["<<e.v<<"]="<<dist[e.v]<<" j="<<j<<endl;
}
if(dist[s]>1.0)//可以套匯
{
flag=true;
return;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int i,j,cnt=0;
while(cin>>n&&n)
{
string name[30];
for(i=0; i<n; ++i)
cin>>name[i];//儲存貨幣名稱
cin>>m;
for(i=0; i<m; ++i)
{
string a,b;
double c;
cin>>a>>c>>b;//讀入匯率
for(j=0; j<n; ++j)
{
if(a==name[j])
edge[i].u=j;
if(b==name[j])
edge[i].v=j;
}
edge[i].w=c;
}
for(i=0; i<n; ++i)
{
flag=false;
Bellman(i);//頂點i到其他頂點
if(flag) break;
}
//cout<<"i="<<i<<" "<<dist[i]<<endl;
if(flag) cout<<"Case "<<++cnt<<": "<<"Yes"<<endl;
else cout<<"Case "<<++cnt<<": "<<"No"<<endl;
}
return 0;
}
/**
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
/**
3 3
0 1 0.5
1 2 10.0
2 0 0.21
3 6
0 1 0.5
0 2 4.9
1 2 10.0
1 0 1.99
3 1 0.09
2 0 0.19
**/
相關文章
- POJ 3169(Bellman-Ford演算法,差分約束系統)演算法
- A星、Floyod、Bellman-Ford
- POJ1860 Currency Exchange【Bellman_ford演算法:判斷是否有正環】演算法
- bellman-ford 單源最短路問題 圖解圖解
- 851. spfa求最短路(用佇列優化bellman——ford演算法)佇列優化演算法
- Python 圖_系列之縱橫對比 Bellman-Ford 和 Dijkstra 最短路徑演算法Python演算法
- 演算法專題 | 10行程式碼實現的最短路演算法——Bellman-ford與SPFA演算法行程
- poj 2031
- poj 3461
- poj 3278 BFS
- POJ 2975 Nim
- poj3417
- POJ 1089 Intervals
- POJ 3414 Pots
- Network(POJ-1144)
- POJ 2553 The Bottom of a Graph
- POJ 1861 Network (Kruskal)
- Apple Catching POJ - 2385APP
- POJ 1442 Black Box
- POJ 2799 IP Networks
- POJ3259-WormholesWorm
- POJ3414-Pots
- 【BFS】poj 3414 Pots
- 200程式碼寫一套屬於自己的事件匯流排(EventBus)庫事件
- Road Construction(POJ-3352)Struct
- Redundant Paths(POJ-3177)
- The Cow Prom(POJ-3180)
- Network of Schools(POJ-1236)
- POJ3414 Pots【BFS】
- POJ 3071 Football(概率DP)
- [poj1275][Cashier Employment]
- POJ - 1125 Stockbroker Grapevine(Java)Java
- Dungeon Master(POJ-2251)AST
- POJ 1611 The Suspects 圖論圖論
- POJ 3267 The Cow Lexicon(dp)
- POJ3126-Prime Path
- POJ1426-Find The Multiple
- POJ2251 Dungeon MasterAST
- POJ 2184 (01揹包)