[ACM] hdu 1217 Arbitrage (bellman_ford最短路,判斷是否有正權迴路或Floyed)
Arbitrage
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 解題思路:
方法1:
一種貨幣經過兌換其它的貨幣,經過幾輪兌換,最終回到該貨幣,使其價值變大,這就是“套利”。採用bellman_ford演算法,不是求對短路,求最大獲利,在bellman演算法上改動。判斷是否含有正權迴路。
程式碼:
方法二:#include <iostream> #include <string.h> using namespace std; const int maxn=35; double dis[maxn]; string str[maxn]; int nodeNum,edgeNum; struct Edge { int s,e; double w; }edge[maxn*maxn]; bool bellman_ford(int start) { for(int i=1;i<=nodeNum;i++) dis[i]=0;//改動1 dis[start]=1; bool ok; for(int i=1;i<=nodeNum-1;i++) { ok=0; for(int j=1;j<=edgeNum;j++) { if(dis[edge[j].s]*edge[j].w>dis[edge[j].e])//改動2 { dis[edge[j].e]=dis[edge[j].s]*edge[j].w; ok=1; } } if(!ok) break; } for(int i=1;i<=edgeNum;i++) if(dis[edge[i].s]*edge[i].w>dis[edge[i].e]) return true; return false; } int main() { int c=1; while(cin>>nodeNum&&nodeNum) { for(int i=1;i<=nodeNum;i++) cin>>str[i]; cin>>edgeNum; string from,to; double w; for(int i=1;i<=edgeNum;i++) { cin>>from>>w>>to; int j,k; for(j=1;j<=nodeNum;j++) if(from==str[j]) break; for(k=1;k<=nodeNum;k++) if(to==str[k]) break; edge[i].s=j; edge[i].e=k; edge[i].w=w; } if(bellman_ford(1)) cout<<"Case "<<c++<<": Yes"<<endl; else cout<<"Case "<<c++<<": No"<<endl; } return 0; }
建立圖,鄰接矩陣,求任意兩條邊之間的最短路,如果dis[i][i]>1 的話,說明存在正權迴路。程式碼中使用到了map, 字串到整型編號的對映
程式碼:
#include <iostream> #include <map> #include <string.h> using namespace std; const int maxn=40; string str[maxn]; double mp[maxn][maxn]; int nodeNum,edgeNum; void floyed() { for(int k=1;k<=nodeNum;k++) for(int i=1;i<=nodeNum;i++) for(int j=1;j<=nodeNum;j++) { if(mp[i][j]<mp[i][k]*mp[k][j]) mp[i][j]=mp[i][k]*mp[k][j]; } } int main() { int c=1; while(cin>>nodeNum&&nodeNum) { map<string,int>st; for(int i=1;i<=nodeNum;i++) { cin>>str[i]; st[str[i]]=i; } for(int i=1;i<=nodeNum;i++) for(int j=1;j<=nodeNum;j++) { if(i==j) mp[i][j]=1; else mp[i][j]=0; } cin>>edgeNum; string from,to;double w; for(int i=1;i<=edgeNum;i++) { cin>>from>>w>>to; mp[st[from]][st[to]]=w; } floyed(); bool ok=0; for(int i=1;i<=nodeNum;i++) if(mp[i][i]>1) ok=1; if(ok) cout<<"Case "<<c++<<": Yes"<<endl; else cout<<"Case "<<c++<<": No"<<endl; } return 0; }
相關推薦
[ACM] hdu 1217 Arbitrage (bellman_ford最短路,判斷是否有正權迴路或Floyed)
Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency in
HDOJ 1217 Arbitrage(擬最短路,floyd算法)
same double script strong ram tar per bre center Arbitrage Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/
bellman-ford演算法——最短路問題,判斷是否存在負權迴路或正權迴路
Dijkstra演算法是處理單源最短路徑的有效演算法,但它侷限於邊的權值非負的情況,若圖中出現權值為負的邊,Dijkstra演算法就會失效,求出的最短路徑就可能是錯的。 這時候,就需要使用其他的演算法來求解最短路徑,Bellman-Ford演算法就是其中最常用的一個
1217 Arbitrage(最短路)
Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into mo
ACM:最短路,dijkstra,鄰接表的建立,使用鄰接表跟優先佇列的dijkstra,Bellman-Ford,Floyd。。
(一)dijkstra,鄰接矩陣 所有邊權均為正,不管有沒有環,求單個源點出發,到所有節點的最短路。該方法同時適用於有向圖和無向圖。 #include <iostream> #include <string> #include <stack&g
L. Magical Girl Haze(ACM-ICPC 2018 南京賽區網路預賽,分層最短路,堆優化的dijkstra)
描述 There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distan
HDU-1217 Arbitrage (有向圖最大環[Floyd])
Arbitrage Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Description Arbitrage is the us
hdu 1217 Arbitrage(floyd 每對頂點間的“最短距離”)
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
HDU 2680 Choose the best route【多源最短路,Dijkstra+spfa】
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13024
最短路,dijstra算法
ear 代碼 大於 鏈表 include n) clear esp als #include<iostream> #include<stdio.h> #include<math.h> #include<vector> usi
HDU 6071 同余最短路 spfa
res targe star pragma std sca nts push bits Lazy Running Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/O
hdu 2544 單源最短路問題 dijkstra+堆優化模板
尋找 問題 col .cn 入隊 ron ava iss cto 最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su
HDU 5889 Barricade(最短路+最小割)
ges bool rri mes 最大流 max pair ear names http://acm.hdu.edu.cn/showproblem.php?pid=5889 題意: 給出一個圖,帝國將軍位於1處,敵軍位於n處,敵軍會選擇最短路到達1點。現在帝國將軍要在路
hdu 5521 Meeting(最短路)
names n) can style air ima pid hid con 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 題意:有1-n共n個點,給出m個塊(完全圖),並知道塊內各點之間互相到達花費時
k短路模板(洛谷P2483 [SDOI2010]魔法豬學院)(k短路,最短路,左偏樹,priority_queue)
持久化 ans main 路徑 什麽 problem node with define 你谷數據夠強了,以前的A*應該差不多死掉了。 所以,小夥伴們快來一起把YL頂上去把!戳這裏! 俞鼎力的課件 需要掌握的內容: Dijkstra構建最短路徑樹。 可持久化堆(使用左偏樹,因
HDU - 5521 巧妙地最短路
題意:n個點,m塊,塊的意思就是說,在塊中的點任意兩點的距離都是t,問分別從1點和n點走到某個點,這個點的花費就是二者較大的,問這n個點花費最小是多少,並按字典序列印序號 思路:這題頭疼的就是不知道怎麼建圖,暴力建圖會超記憶體,有一個巧妙的方法是 將這個塊中的點全部連到一個點上,每條邊花
POJ 2240 Arbitrage(Floyd最短路)
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22202 Accepted: 9426 Description Arbitrage is the use of d
The Doors(幾何+最短路,好題)
The Doors http://poj.org/problem?id=1556 Time Limit: 1000MS Memory Limit: 10000K Total Submis
【CodeForces - 144D】Missile Silos(單源最短路,列舉中間邊,列舉情況可能性)
題幹: A country called Berland consists of n cities, numbered with integer numbers from 1to n. Some of them are connected by bid
1018 Public Bike Management - 求符合要求的最短路,並列印
程式碼參考:https://www.liuchuo.net/archives/2373 要注意的問題: 用dijk求最短路的時候,若要記錄最短路是什麼,則用pre陣列,記錄當前節點的前一個節點是什麼,我發現在初始化e陣列的時候,我習慣性的把e[i][i]=0那麼在dijk的鬆弛操作的for迴