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 ...
随机推荐
- 使用grunt实现web自动化
1.grunt作用 实现压缩.编译.单元测试等重复性工作 2.需要安装的软件 第一步:从官网获取nodejs的软件包,直接双击进行安装(windows下) 第二步:打开 ...
- 使用Microsoft.ExceptionMessageBox.dll捕获WinForm程序中异常信息并弹窗显示
WinForm程序开发中,在开发模式下对于异常的处理一般都是通过调试的方式来查找异常发生的未知与原因. 下面以“除数为0”的情况来具体说明. Button按钮事件如下: private void bu ...
- 关于利用input的file属性在页面添加图片的问题
在页面添加图片涉及到兼容的问题怎么解决兼容问题呢?请看下面分析: 在IE浏览器上面我们能直接通过获取其input的value值来获取其图片的路径. 在火狐和谷歌需要用createObjectURL(( ...
- Spring学习笔记①
我觉得Spring之所以发展的好,主要是理论研究与实践是并轨的,能跟得上时代的步伐,尤其是基础理论的研究(可能是最近看三体看多了,对基础理论非常崇拜).微服务的实现啊,RESTful的实现,对应的Sp ...
- java配置文件的读写
最近在做一个爬虫项目时,用到了读写配置文件的方法,记录下来以后可能用的到. Properties pro = new Properties(); boolean IsFirst = true; //从 ...
- 有关ospf抓包
有关ospf抓包 1.相关的路由器为这样子的: 路由器都运行了ospf: 抓包的链路为GE0/0/2 , 2.抓包图: 从图上我们可以看到,protocol info 这一栏里面出现了: hello ...
- android:layout_gravity和android:gravity属性的区别1
一.介绍: gravity的中文意思就是"重心",就是表示view横向和纵向的停靠位置 (1).android:gravity:是对view控件本身来说的,是用来设置view本身的 ...
- 分享小知识:善用Group By排序
以下列举了公用表/临时表/聚合函数三个因素为例子(覆盖索引因素除外,有利用此类索引都会以索引顺序) 环境: Microsoft SQL Server 2014 (SP1-GDR) (KB319472 ...
- 【转】Django HTTP请求的处理流程
Django 和其他 Web 框架的 HTTP 处理的流程大致相同,Django 处理一个 Request 的过程是首先通过中间件,然后再通过默认的 URL 方式进行的.我们可以在 Middlewar ...
- C语言sqrt函数
引入头文件: # include <math.h> sqrt用来求给定值的平方根 double sqrt (double x) 在使用GCC编译时 加入 -lm参数