日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Arbitrage——判断正环Bellman-Ford/SPFA

發布時間:2023/11/30 63 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Arbitrage——判断正环Bellman-Ford/SPFA 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【題目描述】

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 USDollar3 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 USDollar0

Sample Output

Case 1: Yes Case 2: No

【題目分析】
我們如果能夠判斷有一個環在增加就可以,因此我們可以用Bellman-Ford算法或者SPFA算法。
因為貨幣是字符串,用map比較好
跑出來的結果SPFA果然還是比Bellman-Ford快那么一點。

【AC代碼】

#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<cmath> #include<climits> #include<queue> #include<vector> #include<set> #include<map> using namespace std;typedef long long ll; const int MAXN=35; const int MAXM=1e5+5; map<string,int> name; string t,t1,t2; double r; struct node {int u;int v,next;double w; }Edge[MAXM],E; double dis[MAXN]; int vis[MAXN]; int times[MAXN]; int head[MAXN]; int tot; int n,m;inline void AddEdge(int u,int v,double w) {tot++;//Edge[tot].u=u;Edge[tot].v=v; Edge[tot].w=w;Edge[tot].next=head[u]; head[u]=tot; }bool Bellman() {memset(dis,0,sizeof(dis));dis[1]=1.0;int T=n; bool flag;while(T--){flag=false;for(int i=0;i<tot;i++){E=Edge[i];if(dis[E.v]<dis[E.u]*E.w){dis[E.v]=dis[E.u]*E.w;flag=true;}}if(!flag) break;}return T==-1; }bool SPFA() {memset(vis,0,sizeof(vis));memset(times,0,sizeof(times));memset(dis,0,sizeof(dis));queue<int> q;int x,v;dis[1]=1.0; q.push(1);vis[1]=true;while(!q.empty()){x=q.front(); q.pop(); vis[x]=false;for(int i=head[x];i;i=Edge[i].next){v=Edge[i].v;if(dis[v]<dis[x]*Edge[i].w){dis[v]=dis[x]*Edge[i].w;if(!vis[v]){q.push(v); vis[v]=true;times[v]++;if(times[v]>=n) return true;}}}}return false; }int main() {int Case=0;while(~scanf("%d",&n) && n){Case++;for(int i=1;i<=n;i++){cin>>t;name[t]=i;}scanf("%d",&m);tot=0;memset(head,0,sizeof(head));for(int i=1;i<=m;i++){cin>>t1>>r>>t2;AddEdge(name[t1],name[t2],r);}//if(Bellman())if(SPFA()){printf("Case %d: Yes\n",Case);}else{printf("Case %d: No\n",Case);}}return 0; }

總結

以上是生活随笔為你收集整理的Arbitrage——判断正环Bellman-Ford/SPFA的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。