POJ countries in War 3114

Source: Internet
Author: User

Main topic:

Given some cities, and then give some road letters to send letters, A,B,H representative to send letter a city to B city needs h hours.

If not directly can be sent to, you can first through another city to arrive, such as a, B can send a letter, b,c can send a letter, then, A,c can also send a letter.

Among the two cities, if you can send letters to each other, then the two cities belong to a country, the letter can be emailed, so the time required is 0.

There are k queries in the topic, enter a, B, ask the minimum amount of time to send a letter

Topic Analysis:

First the strong unicom and then the composition to find the shortest way.

#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<cstdlib>#include<algorithm>#include<vector>#include<queue>UsingNamespace std;#defineINF 0X7FFFFFF#defineMAXN 510typedef longlong LL;#defineMin (A, B) (A&LT;B?A:B)#defineMOD 1000000007intm, N, Time, top, ans;intSTACK[MAXN], DFN[MAXN], LOW[MAXN], BLOCKS[MAXN];BOOLInstack[maxn];typedefstructnode{intE, W; Node (intE=0,intw=0): E (E), W (w) {}}node;vector<vector<node> >G;vector<vector<node> >G2;voidinit () {memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); Ans= time = top =0;    G.clear (); G.resize (n+2);    G2.clear (); G2.resize (n+2);}voidSPFA () {}voidTarjan (intu) {Low[u]= Dfn[u] = + +Time ; Stack[top++] =u; Instack[u]=true; intLen =g[u].size ();    Node V;  for(intI=0; i<len; i++) {v=G[u][i]; if( !LOW[V.E])            {Tarjan (V.E); Low[u]=min (Low[u], low[v.e]); } elseif (INSTACK[V.E]) Low[u]=min (Low[u], dfn[v.e]); }    intK; if(Dfn[u] = =Low[u]) {         Do{k= stack[--top]; INSTACK[K]=false; BLOCKS[K]=ans; } while(U! =k); Ans++; }}intSPFA (intStar,intEnd) {    intDIST[MAXN]; BOOLVIS[MAXN]; memset (Vis,false,sizeof(VIS));  for(intI=0; i<ans; i++) Dist[i]=INF; Dist[star]=0; Queue<node>Q;    Node P, Pn; Q.push (Node (Star,0) );  while(Q.size ()) {P=Q.front ();        Q.pop (); VIS[P.E]=true; intLen =g2[p.e].size ();  for(intI=0; i<len; i++) {Pn=G2[p.e][i]; if(DIST[PN.E] > DIST[P.E] +PN.W) {DIST[PN.E]= DIST[P.E] +PN.W; if(!VIS[PN.E])            Q.push (PN.E); }        }    }   //for (int i=0; i<ans; i++)//printf ("----%d\n", dist[i]); return dist[end];}voidsolve () {intK, A, B;  for(intI=1; i<=n; i++)    {        if(!Low[i])    Tarjan (i); }     for(intI=1; I <= N; i++)    {        intLen =g[i].size ();        Node V;  for(intj=0; j<len; J + +) {v=G[i][j]; A= Blocks[i], B =BLOCKS[V.E]; if(A! =b) {G2[a].push_back (node (B,V.W)); //printf ("%d->%d,%d\n", A, b, V.W); }}} scanf ("%d",&k);  while(K--) {scanf ("%d%d", &a, &b); A= Blocks[a], B =Blocks[b]; intrel =SPFA (A, b); if(rel = =INF) puts ("Nao e possivel entregar a carta"); Elseprintf ("%d\n", rel); } printf ("\ n");}intMain () { while(SCANF ("%d%d", &n, &m), m+N) {init ();  while(m--)        {            intA, B, C; scanf (" %d%d%d", &a, &b, &c);        G[a].push_back (Node (b,c));    } solve (); } Return0;}

POJ countries in War 3114

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.