xtu summer individual 1 C - Design the city

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算法

 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 }

xtu summer individual 1 C - Design the city

时间: 2024-12-12 14:46:47

xtu summer individual 1 C - Design the city的相关文章

xtu summer individual 6 D - Checkposts

Checkposts Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 427C64-bit integer IO format: %I64d      Java class name: (Any) Your city has n junctions. There are m one-way roads between the junctions. A

xtu summer individual 6 B - Number Busters

Number Busters Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 382B64-bit integer IO format: %I64d      Java class name: (Any) Arthur and Alexander are number busters. Today they've got a competition.

ZOJ 3195 Design the city

倍增法在线LCA..... ZOJ Problem Set - 3195 Design the city Time Limit: 1 Second      Memory Limit: 32768 KB 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,

xtu summer individual 1 E - Palindromic Numbers

E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this

xtu summer individual 5 D - Subsequence

Subsequence Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 353064-bit integer IO format: %I64d      Java class name: Main There is a sequence of integers. Your task is to find the longest subsequence that sa

xtu summer individual 1 A - An interesting mobile game

An interesting mobile game Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 329564-bit integer IO format: %I64d      Java class name: Main XQ,one of the three Sailormoon girls,is usually playing mobile games o

xtu summer individual 2 C - Hometask

Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 154A64-bit integer IO format: %I64d      Java class name: (Any) Sergey attends lessons of the N-ish language. Each lesson he receives a hometas

xtu summer individual 5 F - Post Office

Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 116064-bit integer IO format: %lld      Java class name: Main There is a straight highway with villages alongside the highway. The highway is repres

xtu summer individual 5 A - To Add or Not to Add

To Add or Not to Add Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 231C64-bit integer IO format: %I64d      Java class name: (Any) A piece of paper contains an array of n integers a1, a2, ..., an. Y