zoj——3195 Design the city
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
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
Author: HE, Zhuobin
Source: ZOJ Monthly, May 2009
题目大意:给一个无根树,有q个询问,每个询问3个点,问将这3个点连起来,距离最短是多少
思路:询问3各点之间的最短距离与两个点之间的最短距离相同,我们只需要处理出lca(x,y),lca(y,z), lca(x,z) 然后再求这些点两两之间的最短距离,然后加起来除以2就好了。
鬼畜的输出、、、、
代码:
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define N 51000 using namespace std; int n,m,x,y,z,ans,sum,tot,ans1,ans2,ans3; int fa[N],top[N],size[N],deep[N],head[N],dis[N]; struct Edge { int to,dis,from,next; }edge[N<<]; int add(int x,int y,int z) { tot++; edge[tot].to=y; edge[tot].dis=z; edge[tot].next=head[x]; head[x]=tot; } int read() { ,f=; char ch=getchar(); ; ch=getchar();} +ch-'; ch=getchar();} return x*f; } int lca(int x,int y) { for(;top[x]!=top[y];x=fa[top[x]]) if(deep[top[x]]<deep[top[y]]) swap(x,y); if(deep[x]>deep[y]) swap(x,y); return x; } int dfs(int x) { size[x]=; deep[x]=deep[fa[x]]+; for(int i=head[x];i;i=edge[i].next) { int to=edge[i].to; if(fa[x]!=to) { dis[to]=dis[x]+edge[i].dis; fa[to]=x,dfs(to); size[x]+=size[to]; } } } int dfs1(int x) { ; if(!top[x]) top[x]=x; for(int i=head[x];i;i=edge[i].next) { int to=edge[i].to; if(fa[x]!=to&&size[t]<size[to]) t=to; } if(t) top[t]=top[x],dfs1(t); for(int i=head[x];i;i=edge[i].next) { int to=edge[i].to; if(fa[x]!=to&&to!=t) dfs1(to); } } int begin() { ans=;tot=; memset(fa,,sizeof(fa)); memset(top,,sizeof(top)); memset(dis,,sizeof(dis)); memset(edge,,sizeof(edge)); memset(deep,,sizeof(deep)); memset(size,,sizeof(size)); memset(head,,sizeof(head)); } int main() { while(scanf("%d",&n)!=EOF) { if(sum++) printf("\n"); begin(); ;i<n;i++) { x=read(),y=read(),z=read(); add(x,y,z),add(y,x,z); } dfs(),dfs1(); m=read(); ;i<=m;i++) { x=read(),y=read(),z=read(); ans1=dis[x]+dis[y]-*dis[lca(x,y)]; ans2=dis[x]+dis[z]-*dis[lca(x,z)]; ans3=dis[y]+dis[z]-*dis[lca(y,z)]; ans=(ans1+ans2+ans3)/; printf("%d\n",ans); } } ; }
zoj——3195 Design the city的更多相关文章
- zoj 3195 Design the city LCA Tarjan
题目链接 : ZOJ Problem Set - 3195 题目大意: 求三点之间的最短距离 思路: 有了两点之间的最短距离求法,不难得出: 对于三个点我们两两之间求最短距离 得到 d1 d2 d3 ...
- ZOJ 3195 Design the city (LCA 模板题)
Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terribl ...
- ZOJ 3195 Design the city LCA转RMQ
题意:给定n个点,下面n-1行 u , v ,dis 表示一条无向边和边权值,这里给了一颗无向树 下面m表示m个询问,问 u v n 三点最短距离 典型的LCA转RMQ #include<std ...
- ZOJ - 3195 Design the city
题目要对每次询问将一个树形图的三个点连接,输出最短距离. 利用tarjan离线算法,算出每次询问的任意两个点的最短公共祖先,并在dfs过程中求出离根的距离.把每次询问的三个点两两求出最短距离,这样最终 ...
- zoj 3195 Design the city lca倍增
题目链接 给一棵树, m个询问, 每个询问给出3个点, 求这三个点之间的最短距离. 其实就是两两之间的最短距离加起来除2. 倍增的lca模板 #include <iostream> #in ...
- ZOJ 3195 Design the city 题解
这个题目大意是: 有N个城市,编号为0~N-1,给定N-1条无向带权边,Q个询问,每个询问求三个城市连起来的最小权值. 多组数据 每组数据 1 < N < 50000 1 < Q ...
- ZOJ Design the city LCA转RMQ
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- ZOJ3195 Design the city [2017年6月计划 树上问题04]
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- xtu summer individual 1 C - Design the city
C - Design the city Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu D ...
随机推荐
- Sql Server中清空所有数据表中的记录
Sql Server中清空所有数据表中的记录 清空所有数据表中的记录: 代码如下:exec sp_msforeachtable @Command1 ='truncate table ?'删除所有数据 ...
- WebAPI中Area的使用
很简单,创建area后,添加一下代码到AreaRegistration中即可 context.Routes.MapHttpRoute( name: "api_default", r ...
- How to Configure YUM to Install Packages From Installation ISO (RHEL)
1. Mount RHEL Installation ISO mkdir /media/dvd mount /dev/cdrom /media/dvd 2. Get Media ID with the ...
- sql中的日期时间处理
每个数据库,不同的日期格式化: 1.mysql 2.sqlserver 使用Convert()函数: select convert(char(10),GetDate(),120) as Date 第3 ...
- 11-3 re模块
目录 r 的作用 re模块的常用功能 findall search match split sub 将数字替换成'H' subn 将数字替换成'H',返回元组(替换的结果,替换了多少次) compil ...
- jq进度条
<!doctype html><html><head><meta charset="utf-8"><title>JQue ...
- 简谈Redis
1.为什么使用redis 分析:博主觉得在项目中使用redis,主要是从两个角度去考虑:性能和并发.当然,redis还具备可以做分布式锁等其他功能,但是如果只是为了分布式锁这些其他功能,完全还有其他中 ...
- 微信小程序 设置计时器(setInterval)、清除计时器(clearInterval)
1.wxml代码 <!--index.wxml--> <view class="container"> <button type='primary' ...
- shell脚本语言基本命令
shell脚本语言基本命令脚本:可运行,不需要编译 #vi 1.sh[编写:i(顶格)或o(换一行)]#! /bin/bash##this is a test shell script##Writte ...
- assert.ok()详解
assert.ok(value[, message]) 测试 value 是否为真值.它等同于 assert.equal(!!value, true, message). 如果 value 不是真值, ...