博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZOJ3195 Design the city [2017年6月计划 树上问题04]
阅读量:5133 次
发布时间:2019-06-13

本文共 4199 字,大约阅读时间需要 13 分钟。

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, 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

40 1 10 2 10 3 121 2 30 1 250 1 10 2 11 3 11 4 120 1 21 0 3

Sample Output

3222

Author: HE, Zhuobin

Source: ZOJ Monthly, May 2009

 

三个点的距离等于任意两点间距离加和除以2

 

#include 
#include
#include
#include
inline void read(int &x){ char ch = getchar();char c = ch;x = 0; while(ch < '0' || ch > '9')c = ch, ch = getchar(); while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar(); if(c == '-')x = -x;}inline void swap(int& a, int& b){int tmp = a;a = b;b = tmp;}const int MAXN = 50000 + 10;struct Edge{int u,v,w,next;}edge[MAXN << 1];int head[MAXN], cnt, n, m;inline void insert(int a,int b, int c){edge[++cnt] = Edge{a,b,c,head[a]};head[a] = cnt;}int log2[MAXN], pow2[30];int p[30][MAXN], deep[MAXN], len[MAXN];int b[MAXN];void dfs(int u){ for(int pos = head[u];pos;pos = edge[pos].next) { int v = edge[pos].v; if(b[v])continue; b[v] = true; len[v] = len[u] + edge[pos].w; deep[v] = deep[u] + 1; p[0][v] = u; dfs(v); }}inline void yuchuli(){ b[1] = true; deep[1] = 0; dfs(1); for(register int i = 1;i <= log2[n];i ++) for(register int j = 1;j <= n;j ++) p[i][j] = p[i - 1][p[i - 1][j]];}inline int lca(int va, int vb){ if(deep[va] < deep[vb])swap(va,vb); for(register int i = log2[n];i >= 0;i --) if(deep[va] - pow2[i] >= deep[vb]) va= p[i][va]; if(va == vb)return va; for(register int i = log2[n];i >= 0;i --) { if(p[i][va] != p[i][vb]) { va = p[i][va]; vb = p[i][vb]; } } return p[0][va];}inline int l(int va, int vb){ int k = lca(va, vb); return len[va] + len[vb] - (len[lca(va, vb)] << 1);}int main(){ register int tmp1,tmp2,tmp3; log2[0] = -1; for(register int i = 1;i <= MAXN;++ i)log2[i] = log2[i >> 1] + 1; pow2[0] = 1; for(register int i = 1;i <= 30;++ i)pow2[i] = pow2[i - 1] << 1; bool ok = false; while(scanf("%d", &n) != EOF) { if(ok)putchar('\n'),putchar('\n'); cnt = 0;memset(head, 0, sizeof(head)); memset(edge, 0, sizeof(edge)); memset(deep, 0, sizeof(deep)); memset(p, 0, sizeof(p));m = 0; memset(b, 0, sizeof(b)); memset(len, 0, sizeof(len)); for(register int i = 1;i < n;++ i) { read(tmp1);read(tmp2);read(tmp3); insert(tmp1 + 1, tmp2 + 1, tmp3); insert(tmp1 + 1, tmp1 + 1, tmp3); } yuchuli(); read(m); read(tmp1);read(tmp2);read(tmp3); ++ tmp1;++ tmp2;++ tmp3; printf("%d", (l(tmp1, tmp2) + l(tmp2, tmp3) + l(tmp1, tmp3))>> 1); for(register int i = 2;i <= m;++ i) { read(tmp1);read(tmp2);read(tmp3); tmp1 ++;tmp2 ++;tmp3 ++; int a = l(tmp1, tmp2);int b = l(tmp2, tmp3);int c = l(tmp1, tmp3); printf("\n%d", (l(tmp1, tmp2) + l(tmp2, tmp3) + l(tmp1, tmp3))>> 1); } ok = true; } return 0;}

 

转载于:https://www.cnblogs.com/huibixiaoxing/p/7076239.html

你可能感兴趣的文章
[Django]我的第一个网页,报错啦~(自己实现过程中遇到问题以及解决办法)
查看>>
python-笔记(二)数据类型
查看>>
VS2013的一些常用快捷键
查看>>
如何去各型MCU的官网上下载正确的数据手册
查看>>
Ulink2 "No Ulink Device found" 解决办法
查看>>
php使用imagick进行图像处理
查看>>
前端面试题(3)
查看>>
Hadoop伪分布式搭建(本人新手,欢迎大家多多指导和关照)
查看>>
客户端储存
查看>>
MeshLab编译理解
查看>>
String
查看>>
网站搭建 (第01天) 模型设计
查看>>
[翻译]用 Puppet 搭建易管理的服务器基础架构(3)
查看>>
心情随笔——2012121
查看>>
CF-1099 D. Sum in the tree
查看>>
2017年BackBox5和Ubuntu16.04.1国内更新源
查看>>
NASA: Seeing Jupiter(注视木星)
查看>>
软件开发基本原则(一)—— 策略和因素 (转)
查看>>
Linux 查看系统所有用户
查看>>
实验三——for 语句及分支结构else-if
查看>>