zoj 3195
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3320
离线算法RE了..
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = ;
const int M =;
struct Edge{
int u,v,w,next;
}e[*N];
struct Q_Edge{
int u,v,next,lca;
}eq[*M];
int head[N];
int _head[N];
int dis[N];
int vis[N];
int ances[N];
int father[N];
void addedge(int u,int v,int w,int &k){
e[k].u = u;e[k].v = v;e[k].w= w;
e[k].next = head[u];head[u]=k++;
}
void add_qedge(int u,int v,int &k){
eq[k].u = u;eq[k].v = v;eq[k].lca = -;
eq[k].next = _head[u];_head[u]=k++;
}
int _find(int u){
if(u==father[u]) return father[u];
return father[u] = _find(father[u]);
}
void unions(int u,int v){
int x = _find(u),y = _find(v);
father[x]=y;
}
void Targin(int u){
vis[u] = true;
ances[u]=father[u] = u;
for(int k = head[u];k!=-;k=e[k].next){
if(!vis[e[k].v]){
int v = e[k].v,w = e[k].w;
dis[v] = dis[u]+w;
Targin(v);
unions(v,u);
ances[_find(u)] = u;
}
}
for(int k = _head[u];k!=-;k=eq[k].next){
if(vis[eq[k].v]){
int v = eq[k].v;
eq[k].lca = eq[k^].lca = ances[_find(v)];
}
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF){ memset(head,-,sizeof(head));
memset(_head,-,sizeof(_head)); int tot=;
for(int i=;i<n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w,tot);
addedge(v,u,w,tot);
}
tot = ;
int m;
scanf("%d",&m);
for(int i=;i<m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add_qedge(a,b,tot);
add_qedge(b,a,tot);
add_qedge(a,c,tot);
add_qedge(c,a,tot);
add_qedge(b,c,tot);
add_qedge(c,b,tot); }
memset(vis,,sizeof(vis));
dis[]=;
Targin();
int a[];
for(int i=;i<m;i++){
int s = i*;
a[] = dis[eq[s].u]+dis[eq[s].v]-*dis[eq[s].lca]; ///a->b
a[] = dis[eq[s+].u]+dis[eq[s+].v]-*dis[eq[s+].lca]; ///a->c
a[] = dis[eq[s+].u]+dis[eq[s+].v]-*dis[eq[s+].lca]; ///b->c
//sort(a,a+3);
printf("%d\n",(a[]+a[]+a[])/); }
printf("\n");
} }
zoj 3195的更多相关文章
- zoj 3195 Design the city LCA Tarjan
题目链接 : ZOJ Problem Set - 3195 题目大意: 求三点之间的最短距离 思路: 有了两点之间的最短距离求法,不难得出: 对于三个点我们两两之间求最短距离 得到 d1 d2 d3 ...
- zoj 3195(LCA加强版)
传送门:Problem 3195 https://www.cnblogs.com/violet-acmer/p/9686774.html 题意: 给一个无根树,有q个询问,每个询问3个点(a,b,c) ...
- 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转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 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 ...
- LCA问题的ST,tarjan离线算法解法
一 ST算法与LCA 介绍 第一次算法笔记这样的东西,以前学算法只是笔上画画写写,理解了下,刷几道题,其实都没深入理解,以后遇到新的算法要把自己的理解想法写下来,方便日后回顾嘛>=< R ...
随机推荐
- JavaScript转换与解析JSON的方法
在JavaScript中将JSON的字符串解析成JSON数据格式,一般有两种方式: 一种为使用eval()函数. 使用Function对象来进行返回解析. 使用eval函数来解析,jquery的eac ...
- 背景建模技术(四):视频分析(VideoAnalysis)模块
视频分析模块主要包含两个函数,一个是VideoAnalysis::setup(....),其主要功能就是确定测试的视频是视频文件或摄像头输入亦或是采用命令行参数:第二个函数是VideoAnalysis ...
- NYOJ 740 DP
“炫舞家“ST 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 ST是一个酷爱炫舞的玩家.TA很喜欢玩QQ炫舞,因此TA也爱屋及乌的喜欢玩跳舞机(Dance Danc ...
- LightOJ 1058 - Parallelogram Counting 几何思维
http://www.lightoj.com/volume_showproblem.php?problem=1058 题意:给你顶点,问能够成多少个平行四边形. 思路:开始想使用长度来扫描有多少根,但 ...
- redis启动脚本
#!/bin/sh # # Simple Redis init.d script conceived to work on Linux systems # as it does use of the ...
- Maven-Dependency Mechanism
依赖管理是maven的一个广为人知的特性, 这也是maven擅长的一个领域. 为单一的工程管理依赖不是很难, 但当你着手处理多模块工程和包含数十或数百个模块的应用时, maven可以帮助你很好地保持高 ...
- 三星 C7恢复 出厂设置
http://jingyan.baidu.com/article/c14654134f0fd20bfcfc4c1e.html
- Jmeter-8-FTP测试
1. 此处要深刻理解FTP的用法. 2. Get的时候填写的Remote File 路径/, 此处是相对路径. 实际为/home/user/ 3. Local file 此处要写到具体的文件. 4. ...
- Network of Schools(POJ1326+有向图进行缩点)
题目链接:http://poj.org/problem?id=1236 题目: 题意:对于n个学校,对于一个系统传给某个学校,那么他会传给他得支援学校.从第二开始,每行给你多个数字,表示第i个学校可以 ...
- Commonjs,AMD,CMD和UMD的差异
CommonJS 一种服务器端模块化的规范,Nodejs实现了这种规范,所以就说Nodejs支持CommonJS. CommonJS分为三部分: require 模块加载 exports 模块导出 m ...