题意:与poj1680一样,有不同的换钱渠道,可以完成特定两种货币的交换,并且有汇率,只不过此题是单向边,然后问是否能使财富增加
与poj1680一样,建图之后直接spfa判增值的环即可
1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<queue> 6 #include<map> 7 #include<vector> 8 using namespace std; 9 10 int head[35],nxt[1000],point[1000],size; 11 double val[1000],dist[35]; 12 bool vis[35]; 13 int n,p,t[35]; 14 15 void add(int a,int b,double v){ 16 int i; 17 for(i=head[a];~i;i=nxt[i]){ 18 if(point[i]==b){ 19 if(val[i]<v)val[i]=v; 20 return; 21 } 22 } 23 point[size]=b; 24 val[size]=v; 25 nxt[size]=head[a]; 26 head[a]=size++; 27 } 28 29 void spfa(){ 30 int i; 31 memset(dist,0,sizeof(dist)); 32 memset(vis,0,sizeof(vis)); 33 memset(t,0,sizeof(t)); 34 dist[1]=100; 35 vis[1]=1; 36 queue<int>q; 37 q.push(1); 38 bool f=1; 39 while(!q.empty()&&f){ 40 int u=q.front(); 41 q.pop(); 42 vis[u]=0; 43 for(i=head[u];~i;i=nxt[i]){ 44 int j=point[i]; 45 double v=dist[u]*val[i]; 46 if(dist[j]<v){ 47 dist[j]=v; 48 if(!vis[j]){ 49 q.push(j); 50 vis[j]=1; 51 t[j]++; 52 if(t[j]>n)f=0; 53 } 54 } 55 } 56 } 57 if(f)printf("Case %d: No\n",++p); 58 else printf("Case %d: Yes\n",++p); 59 } 60 61 int main(){ 62 p=0; 63 while(scanf("%d",&n)!=EOF&&n!=0){ 64 map<string,int>M; 65 int i; 66 string tmp; 67 memset(head,-1,sizeof(head)); 68 size=0; 69 for(i=1;i<=n;i++){ 70 cin>>tmp; 71 M[tmp]=i; 72 } 73 int m; 74 scanf("%d",&m); 75 for(i=1;i<=m;i++){ 76 string t1,t2; 77 double a; 78 cin>>t1>>a>>t2; 79 add(M[t1],M[t2],a); 80 } 81 spfa(); 82 } 83 return 0; 84 }
时间: 2024-11-13 09:48:02