C - Design the city

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason of them is the poor design of the roads distribution, and he want to change this situation.

In order to achieve this project, he divide the city up to N regions which can be viewed as separate points. He thinks that the best design is the one that connect all region with shortest road, and he is asking you to check some of his designs.

Now, he gives you an acyclic graph representing his road design, you need to find out the shortest path to connect some group of three regions.

Input

The input contains multiple test cases! In each case, the first line contian a interger N (1 < N < 50000), indicating the number of regions, which are indexed from 0 to N-1. In each of the following N-1 lines, there are three interger Ai, Bi, Li (1 < Li < 100) indicating there's a road with length Li between region Ai and region Bi. Then an interger Q (1 < Q < 70000), the number of group of regions you need to check. Then in each of the following Q lines, there are three interger Xi, Yi, Zi, indicating the indices of the three regions to be checked.

Process to the end of file.

Output

Q lines for each test case. In each line output an interger indicating the minimum length of path to connect the three regions.

Output a blank line between each test cases.

Sample Input

4
0 1 1
0 2 1
0 3 1
2
1 2 3
0 1 2
5
0 1 1
0 2 1
1 3 1
1 4 1
2
0 1 2
1 0 3

Sample Output

3
2 2
2 解题:LCA算法
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxv = ;
const int maxn = ;
struct arc{
int to,w;
};
struct Q{
int index,v;
};
vector<arc>g[maxv];
vector<Q>qy[maxn];
int ans[maxn],uf[maxv],dis[maxv],n,m;
bool vis[maxv];
int _find(int x){
if(uf[x] != x)
uf[x] = _find(uf[x]);
return uf[x];
}
void dfs(int u){
vis[u] = true;
uf[u] = u;
int i,j,k,v;
for(i = ; i < qy[u].size(); i++){
v = qy[u][i].v;
if(vis[v])
ans[qy[u][i].index] += dis[u]+dis[v]-*dis[_find(v)];
}
for(i = ; i < g[u].size(); i++){
v = g[u][i].to;
if(!vis[v]){
dis[v] = dis[u]+g[u][i].w;
dfs(v);
uf[v] = u;
}
}
}
int main(){
int i,j,u,v,w,t = ;
while(~scanf("%d",&n)){
if(t) printf("\n");
for(i = ; i <= n; i++){
g[i].clear();
qy[i].clear();
uf[i] = i;
vis[i] = false;
}
for(i = ; i < n; i++){
scanf("%d%d%d",&u,&v,&w);
g[u].push_back((arc){v,w});
g[v].push_back((arc){u,w});
}
scanf("%d",&m);
for(i = ; i <= m; i++){
scanf("%d %d %d",&u,&v,&w);
qy[u].push_back((Q){i,v});
qy[v].push_back((Q){i,u});
qy[u].push_back((Q){i,w});
qy[w].push_back((Q){i,u});
qy[v].push_back((Q){i,w});
qy[w].push_back((Q){i,v});
}
memset(ans,,sizeof(ans));
dis[] = ;
dfs();
for(i = ; i <= m; i++)
printf("%d\n",ans[i]>>);
t++;
}
return ;
}

xtu summer individual 1 C - Design the city的更多相关文章

  1. zoj——3195 Design the city

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

  2. ZOJ Design the city LCA转RMQ

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

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

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

  5. xtu summer individual 3 F - Opening Portals

    Opening Portals Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  6. xtu summer individual 6 D - Checkposts

    Checkposts Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Orig ...

  7. ZOJ3195 Design the city(LCA)

    题目大概说给一棵树,每次询问三个点,问要把三个点连在一起的最少边权和是多少. 分几种情况..三个点LCA都相同,三个点有两对的LCA是某一点,三个点有两对的LCA各不相同...%……¥…… 画画图可以 ...

  8. ZOJ 3195 Design the city LCA转RMQ

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

  9. ZOJ - 3195 Design the city

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

随机推荐

  1. eclipse快捷键,移动和复制一段代码

    移动代码:alt+上或下箭头 复制加移动代码:ctrl + alt + 上或下箭头

  2. Hadoop调度框架

        大数据协作框架是一个桐城,就是Hadoop2生态系统中几个辅助的Hadoop2.x框架.主要如下: 1,数据转换工具Sqoop 2,文件搜集框架Flume 3,任务调度框架Oozie 4,大数 ...

  3. Unity3d的Sprite Packer用法介绍

    我们用来做sprite 的图片,通常会留有很多空白的地方,我们在画完了sprite之后,这些地方很可能就没有什么作用了.如果想避免这些资源上的浪费,我们可以把各个sprite做成图集,把图片上的空间尽 ...

  4. 【学习笔记】深入理解js原型和闭包(15)——闭包

    前面提到的上下文环境和作用域的知识,除了了解这些知识之外,还是理解闭包的基础. 至于“闭包”这个词的概念的文字描述,确实不好解释,我看过很多遍,但是现在还是记不住. 但是你只需要知道应用的两种情况即可 ...

  5. mac下安装nodejs

    下载 https://nodejs.org/en/ 安装 一步步继续就ok 验证 npm -v node -v Done!

  6. 初用emmet

    下载emmet的pspad插件emmet.js.复制到pspad目录下的 script\JScript 文件夹. 输入 ul#nav>li.item$*4>{Item $} 但是没反应. ...

  7. sql server 全部错误号详释

    0 操作成功完成. 1 功能错误. 2 系统找不到指定的文件. 3 系统找不到指定的路径. 4 系统无法打开文件. 5 拒绝访问. 6 句柄无效. 7 存储控制块被损坏. 8 存储空间不足,无法处理此 ...

  8. H5拖拽事件的完整过程和语法

    <!DOCTYPE HTML> <html> <head> <style type="text/css"> #div1 { widt ...

  9. 洛谷 P1618 三连击(升级版)

    题目描述 将1,2,…,9共9个数分成三组,分别组成三个三位数,且使这三个三位数的比例是A:B:C,试求出所有满足条件的三个三位数,若无解,输出“No!!!”. //感谢黄小U饮品完善题意 输入输出格 ...

  10. 安装 配置 IIS

    一  .找到    控制面板 ------  在程序和功能  -----打开或关闭window 功能 :这里可能要等一下 才会显示 . 二‘.找到  Internet信息服务 ,勾选大部分如下图:点击 ...