Test instructions: Like poj1680, there are different ways to exchange money, you can complete a particular two currencies, and there is exchange rate, but the problem is one-way side, and then ask whether it can increase the wealth
As with poj1680, the SPFA of the value-added rings can be
1#include <iostream>2#include <cstdio>3#include <string>4#include <cstring>5#include <queue>6#include <map>7#include <vector>8 using namespacestd;9 Ten inthead[ *],nxt[ +],point[ +],size; One Doubleval[ +],dist[ *]; A BOOLvis[ *]; - intn,p,t[ *]; - the voidAddintAintBDoublev) { - inti; - for(i=head[a];~i;i=Nxt[i]) { - if(point[i]==b) { + if(VAL[I]<V) val[i]=v; - return; + } A } atpoint[size]=b; -val[size]=v; -nxt[size]=Head[a]; -head[a]=size++; - } - in voidSPFA () { - inti; tomemset (Dist,0,sizeof(Dist)); +memset (Vis,0,sizeof(Vis)); -memset (T,0,sizeof(t)); thedist[1]= -; *vis[1]=1; $queue<int>Q;Panax NotoginsengQ.push (1); - BOOLf=1; the while(!q.empty () &&f) { + intu=Q.front (); A Q.pop (); thevis[u]=0; + for(i=head[u];~i;i=Nxt[i]) { - intj=Point[i]; $ Doublev=dist[u]*Val[i]; $ if(dist[j]<v) { -dist[j]=v; - if(!Vis[j]) { the Q.push (j); -vis[j]=1;Wuyit[j]++; the if(t[j]>n) f=0; - } Wu } - } About } $ if(f) printf ("Case %d:no\n",++p); - Elseprintf"Case %d:yes\n",++p); - } - A intMain () { +p=0; the while(SCANF ("%d", &n)!=eof&&n!=0){ -map<string,int>M; $ inti; the stringtmp; thememset (head,-1,sizeof(head)); theSites0; the for(i=1; i<=n;i++){ -Cin>>tmp; inm[tmp]=i; the } the intm; Aboutscanf"%d",&m); the for(i=1; i<=m;i++){ the stringt1,t2; the DoubleA; +Cin>>t1>>a>>T2; - Add (m[t1],m[t2],a); the }Bayi SPFA (); the } the return 0; -}
View Code
poj2240 Shortest circuit judgment ring