Poj 3114 (korasaju algorithm and dij algorithm)

Source: Internet
Author: User
Countries in war
Time limit:1000 ms   Memory limit:65536 K
Total submissions:2407   Accepted:751

Description

In the year 2050, after different attempts of the UN to maintain peace in the world, the Third World War broke out. the importance of industrial, inclucial and military secrets obliges all the countries to use extremely sophisticated espionage services, so that each city in the world has at least one spy of each country. these spies need to communicate with other spies, informers as well as their headquarters during their actions. unluckily there doesn't exist a secure way for a spy to communicate during the war period, therefore the messages are always sent in code so that only the addressee is able to read the message and understand its meaning.

The spies use the only service that functions during the war period, the post. each city has a postal agency where the letters are sent. the letters can be sent directly to their destination or to other postal agencies, until the letter arrives at the Postal agency of the destination city, if possible.

The postal agency in City A can send a printed letter to the postal agency in City B if there is an agreement on sending letters, which determines the time, in hours, that a letter takes to reach City B from City A (and not necessarily the opposite ). if there is no agreement between the agencies a and B, the Agency a can try to send the letter to any agency so that the letter can reach its destination as early as possible

Some agencies are connected with electronic communication media, such as satellites and optical fiber ers. before the war, these connections cocould reach all the agencies, making that a letter cocould be sent instantly. but during the period of hostilities every country starts to control electronic communication and an agency can only send a letter to another agency by electronic media (or instantly) if they are in the same country. two agencies, A and B, are in the same country if a printed letter sent from any one of the agencies can be delivered to the other one.

The espionage service of your country has managed to obtain the content of all the agreements on sending messages existing in the world and desires to find out the minimum time to send a letter between different pairs of cities. are you capable of helping them?

Input

The input contains several test cases. The first line of each test case contains two integer separated by a space,N(1 ≤N≤ 500) andE(0 ≤EN2), indicating the numbers of cities (numbered from 1N) And of agreements on sending messages, respectively. following them, then,ELines, each containing three integers separated by spaces,X,YAndH(1 ≤X,YN, 1 ≤H≤ 1000), indicating that there exist an agreement to send a printed letter from CityXTo CityY, And that such a letter will be delivered inHHours.

After that, there will be a line with an integerK(0 ≤K≤ 100), the number of queries. Finally, there will beKLines, each representing a query and containing two integers separated by a space,OAndD(1 ≤O,DN). You must determine the minimum time to send a letter from CityOTo CityD.

The end of the input is indicatedN= 0.

Output

For each test case your program shold produceKLines of output.I-Th line shoshould contain an integerM, The minimum time, in hours, to send a letter inI-Th query. if there aren't communication media between the cities of the query, you shoshould print "Nao e possivel entregar a Carta" ("It's impossible to deliver the letter ").

Print a blank line after each test case.

Sample Input

4 51 2 52 1 103 4 84 3 72 3 651 21 31 44 34 13 31 2 102 3 13 2 131 33 13 20 0

Sample output

0660Nao e possivel entregar a carta10Nao e possivel entregar a carta0

Source

South America 2006, Brazil subregion

The question is to find the shortest path between two points. If the two points belong to a strongly connected component, the distance is 0.

Korasaju finds the strongly connected component and stores it in the SCC to recreate the graph (I am not reducing the vertex state here, that is, only setting the distance of the same strongly connected component to 0 ), then dij finds the shortest path, and the floyed will time out if it is not scaled down.

AC code:

# Include <iostream> # include <stdio. h ># include <vector >#include <algorithm> using namespace STD; int SCC [505], vis [505]; int G1 [505] [505], g2 [505] [505]; int n, m; vector <int> S; void dfs1 (int u) {vis [u] = 1; for (INT I = 1; I <= N; I ++) {If (! Vis [I] & G1 [u] [I] & G1 [u] [I] <999999) dfs1 (I);} s. push_back (U);} void dfs2 (int u, int K) {SCC [u] = K; For (INT I = 1; I <= N; I ++) {If (! SCC [I] & G2 [u] [I] & G2 [u] [I] <999999) dfs2 (I, K) ;}} void find_scc () {int K = 0; S. clear (); For (INT I = 1; I <= N; I ++) SCC [I] = vis [I] = 0; For (INT I = 1; I <= N; I ++) if (! Vis [I]) dfs1 (I); For (INT I = S. Size ()-1; I> = 0; I --) if (! SCC [s [I]) dfs2 (s [I], ++ K); // recreate the graph for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) {If (SCC [I] = SCC [J]) g1 [I] [J] = 0 ;}} int dij (INT St, int ed) {int dis [505]; for (INT I = 1; I <= N; I ++) {dis [I] = G1 [st] [I]; vis [I] = 0;} vis [st] = 1; for (INT I = 2; I <= N; I ++) {int V, Mn = 999999; For (Int J = 1; j <= N; j ++) {If (! Vis [J] & Mn> dis [J]) {Mn = dis [J]; V = J ;}} vis [v] = 1; if (Mn = 999999) break; For (Int J = 1; j <= N; j ++) {If (! Vis [J] & dis [J]> Mn + G1 [v] [J]) dis [J] = dis [v] + G1 [v] [J];} return dis [ed];} int main () {While (scanf ("% d", & N, & M )! = EOF) {If (n = 0) break; For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) {if (I = J) G1 [I] [J] = G2 [I] [J] = 0; else G1 [I] [J] = G2 [I] [J] = 999999;} while (M --) {int X, Y, h; scanf ("% d", & X, & Y, & H); G1 [x] [Y] = h; g2 [y] [x] = H;} find_scc (); // The kosaraju algorithm // The floyed of the unscaled point Times out, and the point is too long to be adjusted, directly dij // For (int K = 1; k <= N; k ++) // For (INT I = 1; I <= N; I ++) // For (Int J = 1; j <= N; j ++) // If (G1 [I] [k] + G1 [k] [J] <G1 [I] [J]) // G1 [I] [J] = G1 [I] [k] + G1 [k] [J]; scanf ("% D", & M); While (M --) {int X, Y; scanf ("% d", & X, & Y ); int DS = dij (x, y); If (Ds! = 999999) printf ("% d \ n", DS); else {printf ("Nao e possivel entregar a Carta \ n ");}} printf ("\ n");} 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.