BNUOJ 2105 Distance Queries

Distance Queries

Time Limit: 2000ms
Memory Limit: 30000KB
This problem will be judged on  PKU. Original ID: 1986
64-bit integer IO format: %lld      Java class name: Main
 
 
Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle.  He therefore wants to find a path of a more reasonable length.  The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries".  Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms).  Please answer FJ's distance queries as quickly as possible!
 

Input

* Lines 1..1+M: Same format as "Navigation Nightmare"

* Line 2+M: A single integer, K.  1 <= K <= 10,000

* Lines 3+M..2+M+K: Each line corresponds to a distance query and  contains the indices of two farms.
 

Output

* Lines 1..K: For each distance query, output on a single line an integer giving  the appropriate distance.
 

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

Sample Output

13
3
36

Source

 
解题:LCA求树上任意两点间的距离。任意两点间只有一条路啊!不然那还是树么?^_^。。。。
 
LCA(a,b) = c所以d(a,b) = d(a,root)+d(b,root)-2*d(c,root);人字形?嘻嘻!傻逼。。。。。当时我居然不能明白这个
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <climits>
 7 #include <algorithm>
 8 #include <cmath>
 9 #define LL long long
10 #define INF 0x3f3f3f
11 using namespace std;
12 const int maxn = 100010;
13 struct arc{
14     int to,w;
15 };
16 struct query{
17     int to,id;
18 };
19 int n,m,k,ans[maxn],d[maxn],uf[maxn];
20 bool vis[maxn];
21 vector<arc>g[maxn];
22 vector<query>q[maxn];
23 int Find(int x){
24     if(x != uf[x])
25         uf[x] = Find(uf[x]);
26     return uf[x];
27 }
28 void tarjan(int u,int ds){
29     vis[u] = true;
30     d[u] = ds;
31     uf[u] = u;
32     int i;
33     for(i = 0; i < g[u].size(); i++){
34         if(!vis[g[u][i].to]) {tarjan(g[u][i].to,ds+g[u][i].w);uf[g[u][i].to] = u;}
35     }
36     for(i = 0; i < q[u].size(); i++)
37     if(vis[q[u][i].to]){
38         ans[q[u][i].id] = d[u]+d[q[u][i].to]-2*d[Find(q[u][i].to)];
39     }
40 }
41 int main(){
42     int i,j,u,v,w;
43     char ch;
44     while(~scanf("%d %d",&n,&m)){
45         for(i = 0; i <= n; i++){
46             g[i].clear();
47             q[i].clear();
48             d[i] = 0;
49             vis[i] = false;
50         }
51         for(i = 0; i < m; i++){
52             scanf("%d %d %d %c",&u,&v,&w,&ch);
53             g[u].push_back((arc){v,w});
54             g[v].push_back((arc){u,w});
55         }
56         scanf("%d",&k);
57         for(i = 0; i < k; i++){
58             scanf("%d %d",&u,&v);
59             q[u].push_back((query){v,i});
60             q[v].push_back((query){u,i});
61         }
62         tarjan(1,0);
63         for(i = 0; i < k; i++)
64             printf("%d\n",ans[i]);
65     }
66     return 0;
67 }
View Code

 

 
 

转载于:https://www.cnblogs.com/crackpotisback/p/3897409.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值