Poj 3114 countries in war

Source: Internet
Author: User

 

 

Water...

Code:

 

# Include <iostream>
# Include <cstdlib>
# Include <cstring>
# Include <cstdio>
# Include <queue>
Using namespace STD;

Const int maxn = 510;
Const int maxm = 510*510;
Const int INF = 0x3f3f3f;

Struct edge
{
Int V, W;
Int next;
} Edge [maxm], edge2 [maxm];

Int n, m;
Int CNT, cnt2;
Int SCNT, top, TOT;

Int first [maxn], low [maxn], dfn [maxn], stack [maxn], INS [maxn];
Int d [maxn];
Int first2 [maxn];
Int belong [maxm];

Char STR [51] = "Nao e possivel entregar a Carta ";

Void Init ()
{
CNT = cnt2 = 0;
SCNT = Top = tot = 0;
Memset (first,-1, sizeof (first ));
Memset (first2,-1, sizeof (first2 ));
Memset (dfn, 0, sizeof (dfn ));
Memset (INS, 0, sizeof (INS ));
}

Void read_graph (int u, int V, int W)
{
Edge [CNT]. V = V, edge [CNT]. W = W;
Edge [CNT]. Next = first [u], first [u] = CNT ++;
}

Void read_graph2 (int u, int V, int W)
{
Edge2 [cnt2]. V = V, edge2 [cnt2]. W = W;
Edge2 [cnt2]. Next = first2 [u], first2 [u] = cnt2 ++;
}

Void DFS (int u)
{
Int V;
Dfn [u] = low [u] = ++ tot;
Stack [top ++] = u;
INS [u] = 1;
For (int e = first [u]; e! =-1; E = edge [e]. Next)
{
V = edge [e]. V;
If (! Dfn [v])
{
DFS (v );
Low [u] = min (low [u], low [v]);
}
Else if (INS [v])
{
Low [u] = min (low [u], dfn [v]);
}
}
If (dfn [u] = low [u])
{
SCNT ++;
Do
{
V = stack [-- top];
Belong [v] = SCNT;
INS [v] = 0;
} While (u! = V );
}
}

Void Tarjan ()
{
For (INT v = 1; v <= N; V ++) if (! Dfn [v])
DFS (v );
}

Void spfa (int src)
{
Queue <int> q;
Bool INQ [maxn] = {0 };
For (INT I = 1; I <= N; I ++) d [I] = (I = SRC )? 0: INF;
Q. Push (SRC );
While (! Q. Empty ())
{
Int x = Q. Front (); q. Pop ();
INQ [x] = 0;
For (int e = first2 [X]; e! =-1; E = edge2 [e]. Next)
{
Int v = edge2 [e]. V, W = edge2 [e]. W;
If (d [v]> d [x] + W)
{
D [v] = d [x] + W;
If (! INQ [v])
{
INQ [v] = 1;
Q. Push (v );
}
}
}
}
}

Int read_case ()
{
Init ();
Scanf ("% d", & N );
If (! N) return 0;
Scanf ("% d", & M );
While (M --)
{
Int U, V, W;
Scanf ("% d", & U, & V, & W );
Read_graph (U, V, W );
}
Return 1;
}

Void build ()
{
Tarjan ();
For (INT u = 1; U <= N; U ++)
{
For (int e = first [u]; e! =-1; E = edge [e]. Next)
{
Int v = edge [e]. V, W = edge [e]. W;
If (belong [u]! = Belong [v])
{
Read_graph2 (U, V, W );
}
Else read_graph2 (u, v, 0 );
}
}
}

Void solve ()
{
Int Q;
Build ();
Scanf ("% d", & Q );
While (Q --)
{
Int S, E;
Scanf ("% d", & S, & E );
Spfa (s );
If (d [e]! = Inf) printf ("% d \ n", d [e]);
Else printf ("% s \ n", STR );
}
Printf ("\ n ");
}

Int main ()
{
While (read_case ())
{
Solve ();
}
Return 0;
}

 

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.