标签:
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
Case 1: Yes Case 2: No
#include <iostream> #include <cstdio> #include <map> #include <queue> using namespace std; double a[35][35]; double dis[35]; bool vis[35]; int n; bool spfa(int s) { for(int i=1;i<=n;i++) { dis[i]=0; vis[i]=false; } dis[s]=1.0; vis[s]=true; queue<int>q; q.push(s); while(!q.empty()) { int p=q.front(); q.pop(); vis[p]=false; for(int i=1;i<=n;i++) { if(dis[i]<dis[p]*a[p][i]) { dis[i]=dis[p]*a[p][i]; if(!vis[i]) { vis[i]=true; q.push(i); } if(dis[s]>1.0) return true; } } } return false; } int main() { int kase=0; map<string,double>mapp; char s1[35],s2[35]; while(cin>>n,n) { mapp.clear(); 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; } for(int i=1; i<=n; i++) { scanf("%s",s1); mapp[s1]=i; } int t; cin>>t; double d; while(t--) { scanf("%s%lf%s",s1,&d,s2); int x=mapp[s1]; int y=mapp[s2]; a[x][y]=d; } cout<<"Case "<<++kase<<": "; bool flag=true;; for(int i=1;i<=n;i++) { if(spfa(i)) { flag=false; cout<<"Yes"<<endl; break; } } if(flag) cout<<"No"<<endl; } return 0; }
HDU 1217 Arbitrage 【最短路,map+spfa】
标签:
原文地址:http://blog.csdn.net/hurmishine/article/details/52075154