【HDU】2874 Connections between cities 离线LCA

来源:互联网 发布:淘宝怎么搜便宜的东西 编辑:程序博客网 时间:2024/05/17 09:39

Connections between cities

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4304    Accepted Submission(s): 1236


Problem Description
After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, some materials needed can only be produced in certain places. So we need to transport these materials from city to city. For most of roads had been totally destroyed during the war, there might be no path between two cities, no circle exists as well.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
 

Input
Input consists of multiple problem instances.For each instance, first line contains three integers n, m and c, 2<=n<=10000, 0<=m<10000, 1<=c<=1000000. n represents the number of cities numbered from 1 to n. Following m lines, each line has three integers i, j and k, represent a road between city i and city j, with length k. Last c lines, two integers i, j each line, indicates a query of city i and city j.
 

Output
For each problem instance, one line for each query. If no path between two cities, output “Not connected”, otherwise output the length of the shortest path between them.
 

Sample Input
5 3 21 3 22 4 35 2 31 44 5
 

Sample Output
Not connected6
Hint
HintHuge input, scanf recommended.
 

Source
2009 Multi-University Training Contest 8 - Host by BJNU

传送门:【HDU】2874 Connections between cities

题目大意:给你n个城市,m条边,q个查询,每次询问两个城市之间的最短路径的长度,存在输出长度,否则输出Not connected。(2<=n<=10000, 0<=m<10000, 1<=c<=1000000)。

题目分析:用离线LCA处理即可。
不过要注意内存方面。。。我几乎就爆了。
一开始ans初始化为0,然后就wa了。。后来发现当询问的点对是同一个点的时候,我是判为无解的。。。改了改,AC了。。。

代码如下:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define clear( a , x ) memset ( a , x , sizeof a )typedef long long LL ;const int MAXN = 10005 ;const int MAXQ = 1000005 ;const LL INF = 1e15 ;struct Edge {int v , c , n ;Edge ( int var = 0 , int cap = 0 , int next = 0 ) :v ( var ) , c ( cap ) , n ( next ) {}} ;Edge edge[MAXN << 1] , qedge[MAXQ << 1] ;int adj[MAXN] , qadj[MAXN] ;int cntE , qcntE ;int p[MAXN] ;LL d[MAXN] ;LL ans[MAXQ] ;int vis[MAXN] ;int find ( int x ) {int o = x , ans , tmp ;while ( p[o] != o )o = p[o] ;ans = o ;o = x ;while ( p[o] != o ) {tmp = p[o] ;p[o] = ans ;o = tmp ;}return ans ;}void init () {cntE = qcntE = 0 ;clear ( vis , 0 ) ;clear ( adj , -1 ) ;clear ( qadj , -1 ) ;clear ( ans , -1 ) ;REP ( i , MAXN )d[i] = INF ;}void addedge ( int u , int v , int c ) {edge[cntE] = Edge ( v , c , adj[u] ) ;adj[u] = cntE ++ ;edge[cntE] = Edge ( u , c , adj[v] ) ;adj[v] = cntE ++ ;}void qaddedge ( int u , int v , int c ) {qedge[qcntE] = Edge ( v , c , qadj[u] ) ;qadj[u] = qcntE ++ ;qedge[qcntE] = Edge ( u , c , qadj[v] ) ;qadj[v] = qcntE ++ ;}void Tarjan ( int u , int idx ) {vis[u] = idx ;p[u] = u ;for ( int i = adj[u] ; ~i ; i = edge[i].n ) {int v = edge[i].v ;if ( !vis[v] ) {d[v] = min ( d[v] , d[u] + edge[i].c ) ;Tarjan ( v , idx ) ;p[v] = u ;}}for ( int i = qadj[u] ; ~i ; i = qedge[i].n ) {int v = qedge[i].v ;if ( vis[v] == idx )ans[qedge[i].c] = d[v] + d[u] - 2 * d[find ( v )] ;}}int read () {int x = 0 ;char c = ' ' ;while ( c < '0' || c > '9' )c = getchar () ;while ( c >= '0' && c <= '9' ) {x = x * 10 + c - '0' ;c = getchar () ;}return x ;}void work () {int n , m , q ;int u , v , c ;while ( ~scanf ( "%d%d%d" , &n , &m , &q ) ) {init () ;while ( m -- ) {u = read () , v = read () , c = read () ;addedge ( u , v , c ) ;}REP ( i , q ) {u = read () , v = read () ;qaddedge ( u , v , i ) ;}REPF ( i , 1 , n )if ( !vis[i] ) {d[i] = 0 ;Tarjan ( i , i ) ;}REP ( i , q ) {if ( ~ans[i] )printf ( "%I64d\n" , ans[i] ) ;elseprintf ( "Not connected\n" ) ;}}}int main () {work () ;return 0 ;}


0 0
原创粉丝点击