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. [转]Java中Date转换大全,返回yyyy-MM-dd的Date类型

    /** * 获取现在时间,这个好用 * * @return返回长时间格式 yyyy-MM-dd HH:mm:ss */ public static Date getSqlDate() { Date s ...

  2. fgetcsv()函数

    fgetcsv()函数.fgetcsv()函数可以读取指定文件的当前行,使用CSV格式解析出字段,并返回一个包含这些字段的数组.语法格式如下:array fgetcsv(resource $handl ...

  3. 生成HTML表格的后台模板代码

    有时候,我们需要在后台拼接生成前端的html表格,一般的做法就是各种string.StringBuilder的拼接(例子省略...),这样的话如果表头不同就没法做到代码的重用,增加代码的冗余,下面我分 ...

  4. 虚方法virtual详解

    虚方法virtual详解   从C#的程序编译的角度来看,它和其它一般的函数有什么区别呢?一般函数在编译时就静态地编译到了执行文件中,其相对地址在程序运行期间是不发生变化的,也就是写死了的!而虚函数在 ...

  5. 第16周翻译:SQL Server中的事务日志管理,级别3:事务日志、备份和恢复

    源自: http://www.sqlservercentral.com/articles/Stairway+Series/73779/ 作者: Tony Davis, 2011/09/07 翻译:刘琼 ...

  6. Windows各个文件夹介绍

    windows文件介绍 总结 ├WINDOWS │ ├-system32(存放Windows的系统文件和硬件驱动程序) │ │ ├-config(用户配置信息和密码信息) │ │ │ └-system ...

  7. 用python+pygame写贪吃蛇小游戏

    因为python语法简单好上手,前两天在想能不能用python写个小游戏出来,就上网搜了一下发现了pygame这个写2D游戏的库.了解了两天再参考了一些资料就开始写贪吃蛇这个小游戏. 毕竟最开始的练手 ...

  8. 检查bug

    用selective_search生成的坐标是(ymin,xmin,ymax,xmax),并且是从1开始的,不是从0 这是cache中的gt数据,明显看到有65535,说明很有可能是0-1变成了655 ...

  9. Windows Server 2012 R2 with Update (x64) - DVD (Chinese-Simplified)

    http://www.msdn.hk/html/2014/1404.html Windows Server 2012 R2 with Update (x64) - DVD (Chinese-Simpl ...

  10. MyBatis基本运行环境

    MyBatis基本运行环境 1. 创建项目 2.拷贝jar加入到项目中build path jar包 3.创建数据库的表及数据添加 USE [mybatis] CREATE TABLE [dbo].[ ...