poj3114 strong connectivity + shortest circuit

Source: Internet
Author: User

Test instructions: There are n cities, between cities can be sent by mail or e-mail messages, known m-mail lines, each line on behalf of A can send mail to B, and spend V time, if several cities can mail each other, then they are in the same country, they can send messages through e-mail, It takes 0 time. There are k queries, and each time you ask for a message from point A to B, the minimum amount of time it takes.

Since multiple cities can send mail to each other, it is not necessary to pass messages to each other in the same country, so it is first strongly connected and then the solution is done for each query. I started to think that strong connectivity after the indentation is directed to the non-circular graph, direct DFS for each query complexity is not very large, but the result of T, and then replaced by each query single source the shortest, then a dropped.

1#include <stdio.h>2#include <string.h>3#include <stack>4#include <queue>5#include <algorithm>6#include <vector>7 using namespacestd;8typedef pair<int,int>PII;9 Ten Const intmaxn=505; One Const intmaxm=250005; A Const intinf=0x3f3f3f3f; -  - inthead[2][maxn],point[2][maxm],nxt[2][maxm],size[2],val[2][MAXM]; the intn,t,scccnt; - intSTX[MAXN],LOW[MAXN],SCC[MAXN]; - intDIS[MAXN]; -stack<int>S; +  - intMinintAintb) {returnA<b?a:b;} +  A voidinit () { atmemset (head,-1,sizeof(head)); -size[0]=size[1]=0; - } -  - voidAddintAintBintVintC=0){ -point[c][size[c]]=b; innxt[c][size[c]]=Head[c][a]; -val[c][size[c]]=v; tohead[c][a]=size[c]++; + } -  the structcmp{//Change priority queue to small Gan *     BOOL operator() (PII a,pii b) { $         returnA.first>B.first;Panax Notoginseng     } - }; the  + voidDij (intSintT) {//incoming starting point and arrival point A     inti; thePriority_queue<pii,vector<pii>,cmp>Q; +Q.push (Make_pair (0, s)); -memset (DIS,0x3f,sizeof(DIS)); $dis[s]=0; $      while(!Q.empty ()) { -PII u=q.top (); - Q.pop (); the         if(U.first>dis[u.second])Continue; -          for(i=head[1][u.second];~i;i=nxt[1][i]) {Wuyi             intj=point[1][i]; the             if(dis[j]>u.first+val[1][i]) { -dis[j]=u.first+val[1][i]; Wu Q.push (Make_pair (dis[j],j)); -             } About         } $     } -     if(Dis[t]==inf) printf ("Nao e possivel entregar a carta\n"); -     Elseprintf"%d\n", Dis[t]); - } A  + voidDfsints) { thestx[s]=low[s]=++T; - S.push (S); $      for(inti=head[0][s];~i;i=nxt[0][i]) { the         intj=point[0][i]; the         if(!Stx[j]) { the Dfs (j); thelow[s]=min (low[s],low[j]); -         } in         Else if(!Scc[j]) { thelow[s]=min (low[s],stx[j]); the         } About     } the     if(low[s]==Stx[s]) { thescccnt++; the          while(1){ +             intu=S.top (); S.pop (); -scc[u]=scccnt; the             if(S==u) Break;Bayi         } the     } the } -  - voidSETSCC () { thememset (STX,0,sizeof(STX)); thememset (SCC,0,sizeof(SCC)); theT=scccnt=0; the      for(intI=1; i<=n;++i)if(!Stx[i]) DFS (i); -      for(intI=1; i<=n;++i) { the          for(intj=head[0][i];~j;j=nxt[0][j]) { the             intk=point[0][j]; the             if(scc[i]!=Scc[k]) {94Add (scc[i],scc[k],val[0][J],1); the             } the         } the     }98 } About  - intMain () {101     intm;102      while(SCANF ("%d", &n)!=eof&&N) {103scanf"%d",&m);104 init (); the          while(m--){106             inta,b,v;107scanf"%d%d%d",&a,&b,&v);108 Add (a,b,v);109         } the SETSCC ();111         intK; thescanf"%d",&k);113          while(k--){ the             intb; thescanf"%d%d",&a,&b); the             if(Scc[a]==scc[b]) printf ("0\n");117             Else{118 Dij (Scc[a],scc[b]);119             } -         }121printf"\ n");122     }123     return 0;124}
View Code

poj3114 strong connectivity + shortest circuit

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.