【POJ】3114 Countries in War 强连通+最短路

Countries in War
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 2236
Accepted: 698

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, commercial 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 fibers. Before the war, these connections could reach all the agencies, making that a letter could 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) and E (0 ≤ EN2), indicating the numbers of cities (numbered from 1 to N) and of agreements on sending messages, respectively. Following them, then, E lines, each containing three integers separated by spaces, X, Y and H (1 ≤ X, YN, 1 ≤ H ≤ 1000), indicating that there exist an agreement to send a printed letter from city X to city Y, and that such a letter will be delivered in H hours.

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

The end of the input is indicated by N = 0.

Output

For each test case your program should produce K lines of output. The I-th line should contain an integer M, the minimum time, in hours, to send a letter in the I-th query. If there aren’t communication media between the cities of the query, you should 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 5
1 2 5
2 1 10
3 4 8
4 3 7
2 3 6
5
1 2
1 3
1 4
4 3
4 1
3 3
1 2 10
2 3 1
3 2 1
3
1 3
3 1
3 2
0 0

Sample Output

0
6
6
0
Nao e possivel entregar a carta

10
Nao e possivel entregar a carta
0

Source

South America 2006, Brazil Subregion

传送门:【POJ】3114 Countries in War

题目大意:给一个n(1 <= n <= 500)个节点的有向图,有m( 0 <= m <= n ^ 2 )条边,每条边都有一个费用,现在给你K( 0 <= K <= 100 )个询问( u , v ) 问从u到v的最小花费。如果图中存在u能到达v且v能到达u,则u到v的费用为0。无解输出Nao e possivel entregar a carta。

题目分析:强连通缩点,建DAG图的时候,不同分量之间的边选择最小花费的那条即可。然后每次询问跑一遍DAG上的最短路就行了。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define clear( a , x ) memset ( a , x , sizeof a )
#define copy( a , x ) memcpy ( a , x , sizeof a )

const int MAXN = 505 ;
const int MAXE = 300000 ;
const int MAXQ = 300000 ;
const int INF = 0x3f3f3f3f ;

struct Edge {
	int v , n , c ;
	Edge ( int var = 0 , int cost = 0 , int next = 0 ) :
		v ( var ) , c ( cost ) , n ( next ) {}
} ;

struct SCC {
	Edge edge[MAXE] ;
	int adj[MAXN] , cntE ;
	int Dfn[MAXN] , Low[MAXN] , ins[MAXN] , scc[MAXN] ;
	int dfs_clock , scc_cnt ;
	int S[MAXN] , top ;
	
	void init () {
		top = 0 ;
		cntE = 0 ;
		scc_cnt = 0 ;
		dfs_clock = 0 ;
		clear ( scc , 0 ) ;
		clear ( Dfn , 0 ) ;
		clear ( ins , 0 ) ;
		clear ( adj , -1 ) ;
	}
	
	void addedge ( int u , int v , int c ) {
		edge[cntE] = Edge ( v , c , adj[u] ) ;
		adj[u] = cntE ++ ;
	}
	
	void Tarjan ( int u ) {
		Dfn[u] = Low[u] = ++ dfs_clock ;
		S[top ++] = u ;
		ins[u] = 1 ;
		for ( int i = adj[u] ; ~i ; i = edge[i].n ) {
			int v = edge[i].v ;
			if ( !Dfn[v] ) {
				Tarjan ( v ) ;
				Low[u] = min ( Low[u] , Low[v] ) ;
			}
			else if ( ins[v] )
				Low[u] = min ( Low[u] , Dfn[v] ) ;
		}
		if ( Low[u] == Dfn[u] ) {
			++ scc_cnt ;
			while ( 1 ) {
				int v = S[-- top] ;
				ins[v] = 0 ;
				scc[v] = scc_cnt ;
				if ( v == u )
					break ;
			}
		}
	}
	
	void find_scc ( int n ) {
		REPF ( i , 1 , n )
			if ( !Dfn[i] )
				Tarjan ( i ) ;
	}
} ;

struct DAG {
	Edge edge[MAXE] ;
	int adj[MAXN] , cntE ;
	int d[MAXN] , in[MAXN] , rin[MAXN] ;
	int Q[MAXQ] ;
	int head , tail ;
	
	void init () {
		cntE = 0 ;
		clear ( rin , 0 ) ;
		clear ( adj , -1 ) ;
	}
	
	void addedge ( int u , int v , int c ) {
		edge[cntE] = Edge ( v , c , adj[u] ) ;
		adj[u] = cntE ++ ;
	}
	
	int solve ( int n , int s , int t ) {
		clear ( d , INF ) ;
		copy ( in , rin ) ;
		head = tail = 0 ;
		REPF ( i , 1 , n )
			if ( !in[i] )
				Q[tail ++] = i;
		d[s] = 0 ;
		while ( head != tail ) {
			int u = Q[head ++] ;
			for ( int i = adj[u] ; ~i ; i = edge[i].n ) {
				int v = edge[i].v ;
				if ( d[v] > d[u] + edge[i].c )
					d[v] = d[u] + edge[i].c ;
				if ( 0 == ( --in[v] ) )
					Q[tail ++] = v ;
			}
		}
		return d[t] == INF ? -1 : d[t] ;
	}
} ;

SCC C ;
DAG G ;
int d[MAXN][MAXN] ;

void read ( int &x ) {
	x = 0 ;
	char c = ' ' ;
	while ( c < '0' || c > '9' )
		c = getchar () ;
	while ( c >= '0' && c <= '9' )
		x = x * 10 + c - '0' , c = getchar () ;
} 

void work () {
	int n , m , q ;
	int u , v , c ;
	while ( ~scanf ( "%d%d" , &n , &m ) && n ) {
		C.init () ;
		G.init () ;
		clear ( d , INF ) ;
		while ( m -- ) {
			read ( u ) , read ( v ) , read ( c ) ;
			C.addedge ( u , v , c ) ;
		}
		C.find_scc ( n ) ;
		REPF ( i , 1 , n ) {
			u = C.scc[i] ;
			for ( int j = C.adj[i] ; ~j ; j = C.edge[j].n ) {
				v = C.scc[C.edge[j].v] ;
				if ( u != v && d[u][v] > C.edge[j].c )
					d[u][v] = C.edge[j].c ;
			}
		}
		REPF ( i , 1 , C.scc_cnt )
			REPF ( j , 1 , C.scc_cnt )
				if ( d[i][j] != INF )
					G.addedge ( i , j , d[i][j] ) , ++ G.rin[j] ;
		read ( q ) ;
		while ( q -- ) {
			read ( u ) ;
			read ( v ) ;
			u = C.scc[u] ;
			v = C.scc[v] ;
			if ( u == v )
				printf ( "0\n" ) ;
			else {
				int ans = G.solve ( C.scc_cnt , u , v ) ;
				printf ( ~ans ? "%d\n" : "Nao e possivel entregar a carta\n" , ans ) ;
			}
		}
		putchar ( '\n' ) ;
	}
}

int main () {
	work () ;
	return 0 ;
}


你可能感兴趣的:(poj,图论)