poj2240 Shortest circuit judgment ring

Source: Internet
Author: User

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]&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.