ZOJ 3195 Design the city 题解
这个题目大意是:
有N个城市,编号为0~N-1,给定N-1条无向带权边,Q个询问,每个询问求三个城市连起来的最小权值。
多组数据 每组数据 1 < N < 50000 1 < Q < 70000;
一道多源最短路的题目,注意题目数据:N、Q都很大
不能考虑Floyd、SPFA、Dijkstra、Bellman-Ford等最短路算法
再看N-1条边,明显构成一棵树,最短路有且只有一条
很明显需要LCA....
我们所熟知的LCA是求两个点的最短路,而该题稍微变形,要求三个点
所以我们可以两两求LCA,然后把三个dist值加起来除以二
而两点的dist值是这样算的:dist[a]+dist[b]-2*dist[LCA(a,b)]
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 200010
#define M 600010
using namespace std;
struct DATA
{
int from,to,dis,next,i;
}road[N],ques[M];
int dist[N],head1[N],head2[M],f[N],ans[M],n,q,len1,len2,fun=;
bool vis[N],book[M],found[N];
void add1(int a,int b,int c,int i)
{
len1++;
road[len1].from=a;
road[len1].to=b;
road[len1].dis=c;
road[len1].i=i;
road[len1].next=head1[a];
head1[a]=len1;
}
void add2(int a,int b,int c,int i)
{
len2++;
ques[len2].from=a;
ques[len2].to=b;
ques[len2].dis=c;
ques[len2].i=i;
ques[len2].next=head2[a];
head2[a]=len2;
}
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
void marge(int x,int y){if(find(x)!=find(y)) f[y]=x;}
void Tarjan(int u,int dev)
{
int e=head1[u],a,b,i,tmp;
while(e!=-)
{
a=road[e].to;
i=road[e].i;
tmp=dev+road[e].dis;
if(!vis[a] && !found[i])
{
found[i]=;
dist[a]=tmp;
Tarjan(a,tmp);
vis[a]=;
marge(u,a);
}
e=road[e].next;
}
e=head2[u];
while(e!=-)
{
a=ques[e].to;
i=ques[e].i;
if(vis[a] && !book[i])
{
book[i]=;
b=find(a);
ans[i]=dist[a]+dist[u]-*dist[b];
}
e=ques[e].next;
}
return ;
}
void init()
{
int i;
len1=,len2=;
for(i=;i<N;i++)
{
road[i].from=road[i].to=road[i].dis=road[i].next=road[i].i=;
f[i]=i;
}
for(i=;i<M;i++)
ques[i].from=ques[i].to=ques[i].dis=ques[i].next=ques[i].i=;
memset(head1,-,sizeof(head1));
memset(head2,-,sizeof(head2));
memset(dist,,sizeof(dist));
memset(vis,,sizeof(vis));
memset(book,,sizeof(book));
memset(found,,sizeof(found));
memset(ans,,sizeof(ans));
return ;
}
int main()
{
// freopen("1.in","r",stdin);
// freopen("test.out","w",stdout);
while(~scanf("%d",&n))
{
if(fun++) printf("\n");
init();
int i,a,b,c,tmp=;
for(i=;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
add1(a,b,c,i);
add1(b,a,c,i);
tmp=min(min(a,b),tmp);
}
scanf("%d",&q);
for(i=;i<=q;i++)
{
scanf("%d%d%d",&a,&b,&c);
add2(a,b,,i);
add2(b,a,,i);
add2(a,c,,i+q);
add2(c,a,,i+q);
add2(b,c,,i+*q);
add2(c,b,,i+*q);
}
Tarjan(tmp,);
for(i=;i<=q;i++)
printf("%d\n",(ans[i]+ans[i+q]+ans[i+*q])/);
}
return ;
}
ZOJ 3195 Design the city 题解的更多相关文章
- zoj——3195 Design the city
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- 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 Tarjan
题目链接 : ZOJ Problem Set - 3195 题目大意: 求三点之间的最短距离 思路: 有了两点之间的最短距离求法,不难得出: 对于三个点我们两两之间求最短距离 得到 d1 d2 d3 ...
- 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 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 ...
随机推荐
- Git基础教程(一)
本教程为学习笔记,github作为最受欢迎的资源库,不可不学!详细教程参见:廖雪峰的官方网站Git教程系列.准备花两篇幅搞定实战总结,闲言碎语少说,脚踏实地求真! 1,Git入门 Git是 ...
- Debian安装Oracle Java步骤
在Debian下安装OpenJDK使用apt命令非常方便的安装,但安装Oracle就需要手动了,这里需了解ln和update-alternatvies命令. ln链接 首先我们来说说linux的链接, ...
- .NET入行之工作前
时间就像轻风一样,刻意感受的时候几乎把你吹倒,不留意的时候又从你身边轻轻飘走了:长此以后,我怕自己会变得麻木,忘记了原来的样子.所以还是决定给自己留点什么,万一哪天忘记了,还可以再翻起来. 工作两年的 ...
- sql查询表说明
一条神奇的sql语句 可以查询到数据库表的说明信息.如下:希望可以帮到大家 SELECT 表名 = case when a.colorder=1 then d.name else '' end, 表说 ...
- Hibernate打印SQL及附加参数
今天在项目运行过程中,一直报一个org.hibernate.exception.GenericJDBCException: could not insert 异常,Root Cause是IBM DB ...
- ubuntu 笔记
Ubuntu学习之路还很长,做个笔记也不亏 terminal tab 补全忽略大小写: 在 /ect/inputrc文件中添加或修改 'set completion-ignore-case on' U ...
- eclipse下配置安装ssm图文教程(web版)
eclipse下配置安装ssm图文教程(web版) 一.安装所需jar包 1.1 mybatis安装包 可以进入GitHub的https://github.com/mybatis/mybatis-3 ...
- Ubuntu部署Jupyter
前言 Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言.在本文中,我们将介绍 Jupyter notebook 的主要特 ...
- 由一个简单需求到Linux环境下的syslog、unix domain socket
本文记录了因为一个简单的日志需求,继而对linux环境下syslog.rsyslog.unix domain socket的学习.本文关注使用层面,并不涉及rsyslog的实现原理,感兴趣的读者可以参 ...
- nodejs npm常用命令 转
npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准.有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包. 1.npm install m ...