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 ...
随机推荐
- 关于docker入门教程
简介:docker入门教程 docker入门教程翻译自docker官方网站的Docker getting started 教程,官方网站:https://docs.docker.com/linux/s ...
- SQL Server性能调优——报表数据库与业务数据库分离
前段时间把公司的主数据库切了,分成业务库和报表库,业务库向报表库进行实时的Replication.这个项目的上线提升了系统的性能和可维护性,现在把设计时的考量和所做的工作重新回顾一下,作为备忘. 项目 ...
- [Windows Server 2012] MySQL移机方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:MySQL ...
- Java之抽象和封装
① 如何从现实世界中抽象出类? 根据软件开发需求: 发现类-->发现类的属性-->发现类的方法 ② 构造方法的作用和特点是什么? 作用:在创建对象时执行一些初始化操作 ...
- java web 学习笔记 - servlet02
1.servlet的跳转 客户端跳转: 通过doget函数中的response参数调用resp.sendRedirect(url); 代码如下 protected void doGet(HttpSer ...
- Windows live writer 2012 测试
升级到win10,居然Windows live writer不能用了,装了好久就是装不上去,wlsetup-web.exe 在线安装失败,wlsetup-all.exe离线安装也失败了. 安装Blog ...
- Java Socket 连接 Client端 和 Server端
Client端: import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;i ...
- mysql字符集乱码问题
程序错误截图如下: 分析:我们mysql数据库没有设置默认编码, 导致创建的库字符集为 latin1,然而我们创建表的时候,指定字符集为其他的,比如utf8 我的解决思路:把数据库的编码修改为utf8 ...
- ZXing.dll 生成二维码 C# winform net4.5
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 字符串匹配算法之BM算法
BM算法,全称是Boyer-Moore算法,1977年,德克萨斯大学的Robert S. Boyer教授和J Strother Moore教授发明了一种新的字符串匹配算法. BM算法定义了两个规则: ...