码迷,mamicode.com
首页 > 其他好文 > 详细

BNUOJ 2105 Distance Queries

时间:2014-08-07 18:28:00      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   os   io   for   

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);人字形?嘻嘻!傻逼。。。。。当时我居然不能明白这个
 
bubuko.com,布布扣
 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

 

 
 

BNUOJ 2105 Distance Queries,布布扣,bubuko.com

BNUOJ 2105 Distance Queries

标签:style   blog   http   color   java   os   io   for   

原文地址:http://www.cnblogs.com/crackpotisback/p/3897409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!