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的更多相关文章

  1. zoj 3195 Design the city LCA Tarjan

    题目链接 : ZOJ Problem Set - 3195 题目大意: 求三点之间的最短距离 思路: 有了两点之间的最短距离求法,不难得出: 对于三个点我们两两之间求最短距离 得到 d1 d2 d3 ...

  2. zoj 3195(LCA加强版)

    传送门:Problem 3195 https://www.cnblogs.com/violet-acmer/p/9686774.html 题意: 给一个无根树,有q个询问,每个询问3个点(a,b,c) ...

  3. zoj——3195 Design the city

    Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...

  4. ZOJ 3195 Design the city LCA转RMQ

    题意:给定n个点,下面n-1行 u , v ,dis 表示一条无向边和边权值,这里给了一颗无向树 下面m表示m个询问,问 u v n 三点最短距离 典型的LCA转RMQ #include<std ...

  5. ZOJ - 3195 Design the city

    题目要对每次询问将一个树形图的三个点连接,输出最短距离. 利用tarjan离线算法,算出每次询问的任意两个点的最短公共祖先,并在dfs过程中求出离根的距离.把每次询问的三个点两两求出最短距离,这样最终 ...

  6. zoj 3195 Design the city lca倍增

    题目链接 给一棵树, m个询问, 每个询问给出3个点, 求这三个点之间的最短距离. 其实就是两两之间的最短距离加起来除2. 倍增的lca模板 #include <iostream> #in ...

  7. ZOJ 3195 Design the city 题解

    这个题目大意是: 有N个城市,编号为0~N-1,给定N-1条无向带权边,Q个询问,每个询问求三个城市连起来的最小权值. 多组数据 每组数据  1 < N < 50000  1 < Q ...

  8. 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 ...

  9. LCA问题的ST,tarjan离线算法解法

    一  ST算法与LCA 介绍 第一次算法笔记这样的东西,以前学算法只是笔上画画写写,理解了下,刷几道题,其实都没深入理解,以后遇到新的算法要把自己的理解想法写下来,方便日后回顾嘛>=< R ...

随机推荐

  1. jQuery考试

    No1: 分析:首先A答案是正确的jQuery中删除元素的方法有a,c,d所以排除B,另外c是清空,d虽然能删除但是不能删除元素所绑定的事件等等. No2: 分析:A是正确的通过get(index)的 ...

  2. POJ 3579 二分

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7687   Accepted: 2637 Descriptio ...

  3. POJ 3107 树形dp

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6812   Accepted: 2390 Descrip ...

  4. HDU3251 最大流(最小割)

    Being a Hero Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. Apache Flume - File通道设计

    原文链接:https://blogs.apache.org/flume/entry/apache_flume_filechannel 说明:翻译在尽量符合原文表达的基础上,尽量保证行文流畅.水平有限, ...

  6. phpstorm license 解决

    http://idea.lanyus.com/ sudo vim /etc/hosts 最后添加:  0.0.0.0 account.jetbrains.com 然后把获得的注册码,复制到,licen ...

  7. C语言预处理

    1.由源码到可执行程序的过程(1)源码.c->(编译)->elf可执行程序(2)源码.c->(编译)->目标文件.o->(链接)->elf可执行程序(3)源码.c- ...

  8. HDU 5876 Sparse Graph BFS+set删点

    Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices s ...

  9. [洛谷P3444] [POI2006]ORK-Ploughing

    洛谷题目链接[POI2006]ORK-Ploughing 题目描述 Byteasar, the farmer, wants to plough his rectangular field. He ca ...

  10. u3d局域网游戏网络(c# socket select 模型)——续

    原文:http://www.cnblogs.com/saucerman/p/5555793.html 因为项目要加语音.语音数据都非常大.所以顺带就把之前写的网络模块一起测试了. 然后发现了一些bug ...