题目描述:
Time Limit: 5000 MS Memory Limit: 65536 K
The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures also conveniently numbered 1..N. Most conveniently of all, cow i is grazing in pasture i. Some pairs of pastures are connected by one of N-1 bidirectional walkways that the cows can traverse. Walkway i connects pastures A_i and B_i (1 <= A_i <= N; 1 <= B_i <= N) and has a length of L_i (1 <= L_i <= 10,000). The walkways are set up in such a way that between any two distinct pastures, there is exactly one path of walkways that travels between them. Thus, the walkways form a tree. The cows are very social and wish to visit each other often. Ever in a hurry, they want you to help them schedule their visits by computing the lengths of the paths between Q (1 <= Q <= 1,000) pairs of pastures (each pair given as a query p1,p2 (1 <= p1 <= N; 1 <= p2 <= N).
INPUT FORMAT:
* Line 1: Two space-separated integers: N and Q * Lines 2..N: Line i+1 contains three space-separated integers: A_i, B_i, and L_i * Lines N+1..N+Q: Each line contains two space-separated integers representing two distinct pastures between which the cows wish to travel: p1 and p2SAMPLE INPUT:
4 22 1 2
4 3 2
1 4 3
1 2
3 2
OUTPUT FORMAT:
* Lines 1..Q: Line i contains the length of the path between the two pastures in query i.SAMPLE OUTPUT:
27
OUTPUT DETAILS:
Query 1: The walkway between pastures 1 and 2 has length 2. Query 2: Travel through the walkway between pastures 3 and 4, then the one between 4 and 1, and finally the one between 1 and 2, for a total length of 7.这道题目是求一棵连通的树中,任意两点之间的距离。
我一开始就是想到 Disjkstra , 因为树中两点距离也一定是最短的,或者说是唯一的,所以 Disjkstra 是一定可以求出来的,如果堆优化,应该不会超时。当中加入一个 dp 数组记录两点之间的距离,不用反复计算。还好 ,152 ms
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std ;
#define INF 0x3f3f3f
typedef pair<int,int> P ;
int N , Q ;
vector<P> One[1001] ;
int dp[1001][1001] ;
int dis[1001] ;
int Dijkstra( int src , int des ){
int i , u , v , w ;
memset( dis , INF , sizeof( dis ) ) ;
priority_queue< P , vector<P> , greater<P> > Q ;
Q.push( P( dis[src] = 0 , src ) ) ;
while( !Q.empty() ){
P top = Q.top() ;
Q.pop() ;
u = top.second ;
if( dis[u] < top.first )
continue ;
for( i = 0 ; i < (int)One[u].size() ; ++i ){
v = One[u][i].first , w = One[u][i].second ;
if( dis[v] > dis[u] + w )
Q.push( P( dis[v] = dis[u] + w , v ) ) ;
}
}
for( i = 1 ; i <= N ; ++i )
dp[src][i] = dp[i][src] = dis[i] ; // 因为是无向图 , 可以记录双向的最短距离
return dis[des] ;
}
int main(){
int i , u , v , w ;
while( ~scanf( "%d%d" , &N , &Q ) ){
for( i = 1 ; i <= N ; ++i ) // 注意要清空
One[i].clear() ;
for( i = 1 ; i <= N-1 ; ++i ){
scanf( "%d%d%d" , &u , &v , &w ) ;
One[u].push_back( P( v , w ) ) ;
One[v].push_back( P( u , w ) ) ;
}
memset( dp , 0 , sizeof( dp ) ) ;
while( Q-- ){
scanf( "%d%d" , &u , &v ) ;
printf( "%d\n" , dp[u][v] ? dp[u][v] : Dijkstra( u , v ) ) ; // dp 类似于记忆
}
}
return 0 ;
}
看到这道题目,我有预感是 LCA(虽然没学过,但我竟然有这感觉,emm ) , 之前没去学这个,看到大佬们的代码,才发现真的更快,开始 LCA 的学习之旅吧。
学习了 Tarjan 之后,在 SCU 3365 一直超时。
今天学习了倍增算法,终于 8ms 过。SCU 3365 倍增的解法