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
**/
相關文章
- A星、Floyod、Bellman-Ford
- POJ 3169(Bellman-Ford演算法,差分約束系統)演算法
- POJ1860 Currency Exchange【Bellman_ford演算法:判斷是否有正環】演算法
- bellman-ford 單源最短路問題 圖解圖解
- 【演算法導論】24.1 Bellman-Ford 演算法演算法
- 851. spfa求最短路(用佇列優化bellman——ford演算法)佇列優化演算法
- hdu1874 暢通工程續 Bellman-Ford演算法SPFA演算法
- Bellman - Ford, SPFA 學習筆記(含有負權的單源最短路徑)筆記
- Bellman Ford+SPFA佇列優化(路徑還原 輸出最短路的路徑)佇列優化
- Python 圖_系列之縱橫對比 Bellman-Ford 和 Dijkstra 最短路徑演算法Python演算法
- 演算法專題 | 10行程式碼實現的最短路演算法——Bellman-ford與SPFA演算法行程
- POJ 1149-PIGS(Ford-Fulkerson 標號法求網路最大流)
- PHPSecurityforDeployersPHP
- Bellmanford與Spfa解決存在負邊權的單源匯最短路問題
- OutOfOrderScannerNextException:wastherearpctiExceptionASTRPC
- Ubuntu下安裝Stanford CoreNLPUbuntu
- Assertion failure in -[UITableView -configureCellForDisplay:forIndexPath:]AIUIViewIndex
- Stanford機器學習課程筆記——SVM機器學習筆記
- POJ 3461 kmpKMP
- poj3417
- 轉:Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]AIUIViewIndex
- 200程式碼寫一套屬於自己的事件匯流排(EventBus)庫事件
- Developing iOS 8 Apps with Swift (stanford)deviOSAPPSwift
- android Studio遇到transformClassesWithDexForDebug錯誤AndroidORM
- Flip Game(POJ 1753)GAM
- POJ 2431 Expedition
- POJ 3414 Pots
- POJ3259-WormholesWorm
- 樹套樹
- DP套DP
- Stanford機器人D-H座標系機器人
- Ford-Fulkerson 標號法求網路最大流
- 微軟John Langford:機器學習解決哪些世界難題?微軟機器學習
- POJ 1562 Oil Deposits
- poj 2478 尤拉函式函式
- POJ 2524 Ubiquitous ReligionsUI
- POJ 3981 字串替換字串
- poj 1276 Cash MachineMac