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. Hadoop,大数据,云计算三者之间的关系

    大数据和云计算是何关系?关于大数据和云计算的关系人们通常会有误解.而且也会把它们混起来说,分别做一句话直白解释就是:云计算就是硬件资源的虚拟化;大数据就是海量数据的高效处理.大数据.hadoop及云计 ...

  2. 复习java数据库操作的总结

    以前学习java数据库操作,学得那叫糊里糊涂,各种JDBC常用的类和接口根本是傻傻分不清啥是干嘛的.只是套着用用吧. 不过这次好歹清楚些了,呜呜,学习有阶段性,多次重复才有好效果,多么痛的领悟. 工程 ...

  3. [LeetCode][Facebook面试题] 通配符匹配和正则表达式匹配,题 Wildcard Matching

    开篇 通常的匹配分为两类,一种是正则表达式匹配,pattern包含一些关键字,比如'*'的用法是紧跟在pattern的某个字符后,表示这个字符可以出现任意多次(包括0次). 另一种是通配符匹配,我们在 ...

  4. ZooKeeper开发者指南(五)

    引言 这个文档是为了想利用ZooKeeper的协调服务来创建分布式应用的开发者提供的指南.它包括概念和实践的信息. 这个文档的一开始的的四部分呈现了不同ZooKeeper高级概念的的讨论.理解Zook ...

  5. nodejs与mongo

    1.连接URL (使用数据用户名与密码连接或不使用连接数据库) npm install mongodb --save var mon = require('mongodb').MongoClient; ...

  6. python socket和简单tcp通信实现

    python 服务端和客户端的简单交互 TCP服务端: 1 创建套接字,绑定套接字到本地IP与端口 s = socket.socket(socket.AF_INET,socket.SOCK_STREA ...

  7. NOJ/HUST 1095 校赛 Just Go 线段树模板题

    Description There is a river, which contains n stones from left to right. These stones are magic, ea ...

  8. Maven:Non-resolvable parent POM: Failure to find错误

    使用Maven编译项目时遇到如下错误: [ERROR] The project dfjr.generic:dfjr-generic:1.0-SNAPSHOT (F:\workspace\DFJR-PE ...

  9. PowerDesigner16 设置导出sql文件的编码

    一: 导出数据库结构sql脚本 Database ->  Generate  Database 在弹窗中选择Format选项卡,修改Encoding,选择自己需要的编码格式. 二:比较数据库差异 ...

  10. 快速排序Quick sort

    快速排序Quick sort 原理,通过一趟扫描将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归 ...