首页 > 其他 > 详细

xtu summer individual 1 C - Design the city

时间:2014-08-01 09:12:51      阅读:411      评论:0      收藏:0      [点我收藏+]
C - Design the city

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

 

Description

Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason of them is the poor design of the roads distribution, and he want to change this situation.

In order to achieve this project, he divide the city up to N regions which can be viewed as separate points. He thinks that the best design is the one that connect all region with shortest road, and he is asking you to check some of his designs.

Now, he gives you an acyclic graph representing his road design, you need to find out the shortest path to connect some group of three regions.

 

Input

 

The input contains multiple test cases! In each case, the first line contian a interger N (1 < N < 50000), indicating the number of regions, which are indexed from 0 to N-1. In each of the following N-1 lines, there are three interger Ai, Bi, Li (1 < Li < 100) indicating there‘s a road with length Li between region Ai and region Bi. Then an interger Q (1 < Q < 70000), the number of group of regions you need to check. Then in each of the following Q lines, there are three interger Xi, Yi, Zi, indicating the indices of the three regions to be checked.

Process to the end of file.

 

Output

 

Q lines for each test case. In each line output an interger indicating the minimum length of path to connect the three regions.

Output a blank line between each test cases.

 

Sample Input

 

4
0 1 1
0 2 1
0 3 1
2
1 2 3
0 1 2
5
0 1 1
0 2 1
1 3 1
1 4 1
2
0 1 2
1 0 3

 

Sample Output

 

3
2

2
2

解题:LCA算法


bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #define LL long long
13 #define INF 0x3f3f3f3f
14 using namespace std;
15 const int maxv = 50010;
16 const int maxn = 70010;
17 struct arc{
18     int to,w;
19 };
20 struct Q{
21     int index,v;
22 };
23 vector<arc>g[maxv];
24 vector<Q>qy[maxn];
25 int ans[maxn],uf[maxv],dis[maxv],n,m;
26 bool vis[maxv];
27 int _find(int x){
28     if(uf[x] != x)
29         uf[x] = _find(uf[x]);
30     return uf[x];
31 }
32 void dfs(int u){
33     vis[u] = true;
34     uf[u] = u;
35     int i,j,k,v;
36     for(i = 0; i < qy[u].size(); i++){
37         v = qy[u][i].v;
38         if(vis[v])
39             ans[qy[u][i].index] += dis[u]+dis[v]-2*dis[_find(v)];
40     }
41     for(i = 0; i < g[u].size(); i++){
42         v = g[u][i].to;
43         if(!vis[v]){
44             dis[v] = dis[u]+g[u][i].w;
45             dfs(v);
46             uf[v] = u;
47         }
48     }
49 }
50 int main(){
51     int i,j,u,v,w,t = 0;
52     while(~scanf("%d",&n)){
53         if(t) printf("\n");
54         for(i = 0; i <= n; i++){
55             g[i].clear();
56             qy[i].clear();
57             uf[i] = i;
58             vis[i] = false;
59         }
60         for(i = 1; i < n; i++){
61             scanf("%d%d%d",&u,&v,&w);
62             g[u].push_back((arc){v,w});
63             g[v].push_back((arc){u,w});
64         }
65         scanf("%d",&m);
66         for(i = 1; i <= m; i++){
67             scanf("%d %d %d",&u,&v,&w);
68             qy[u].push_back((Q){i,v});
69             qy[v].push_back((Q){i,u});
70             qy[u].push_back((Q){i,w});
71             qy[w].push_back((Q){i,u});
72             qy[v].push_back((Q){i,w});
73             qy[w].push_back((Q){i,v});
74         }
75         memset(ans,0,sizeof(ans));
76         dis[0] = 0;
77         dfs(0);
78         for(i = 1; i <= m; i++)
79             printf("%d\n",ans[i]>>1);
80         t++;
81     }
82     return 0;
83 }
View Code

 

xtu summer individual 1 C - Design the city,布布扣,bubuko.com

xtu summer individual 1 C - Design the city

原文:http://www.cnblogs.com/crackpotisback/p/3884144.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!