ZOJ_3195_Design the city(LCA+tarjan)

Design the city

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

Submit
Status

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

题意:在一棵树上,查询u -> v -> w的最短距离。

分析:LCA+tarjan离线算法,模板题。以前经常做的是两点的最短距离,现在换成三点其实是一样的,求出两两之间的最短距离之和,然后除2即可。易得。

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15731

代码清单:

//#pragma comment(linker, "/STACK:102400000,102400000")
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int maxn = 50000 + 5;
const int maxv = 50000 + 5;
const int maxq = 70000 + 5;

struct Edge{
    int u,v,w;
    Edge(){}
    Edge(int u,int v,int w){
        this -> u = u;
        this -> v = v;
        this -> w = w;
    }
}edge[maxq];

struct Q{ int v,id,next; }quary[6*maxq];
struct e{ int v,dis,next; }graph[2*maxn];

int n,m,q;
int a,b,c;
int father[maxn];
bool vis[maxn];
int ans[3*maxq];
int color[maxn];
int depth[maxn];
int nume,numq;
int heade[maxn];
int headq[maxn];
int cases=0;
void init(){
    for(int i=0;i<maxn;i++) father[i]=i;
    memset(ans,-1,sizeof(ans));
    memset(color,0,sizeof(color));
    memset(depth,0,sizeof(depth));
    memset(vis,false,sizeof(vis));
    memset(graph,0,sizeof(graph));
    memset(quary,0,sizeof(quary));
    memset(heade,-1,sizeof(heade));
    memset(headq,-1,sizeof(headq));
    nume=numq=0;
}

void add_E(int u,int v,int dis){
    graph[nume].v=v;
    graph[nume].dis=dis;
    graph[nume].next=heade[u];
    heade[u]=nume++;
}

void add_Q(int u,int v,int id){
    quary[numq].v=v;
    quary[numq].id=id;
    quary[numq].next=headq[u];
    headq[u]=numq++;
}

void input(){
    for(int i=1;i<n;i++){
        scanf("%d%d%d",&a,&b,&c);
        add_E(a,b,c);
        add_E(b,a,c);
    }
    scanf("%d",&q);
    for(int i=1;i<=q;i++){
        scanf("%d%d%d",&a,&b,&c);
        edge[i]=Edge(a,b,c);
        add_Q(a,b,(i-1)*3+1);
        add_Q(b,a,(i-1)*3+1);
        add_Q(a,c,(i-1)*3+2);
        add_Q(c,a,(i-1)*3+2);
        add_Q(b,c,(i-1)*3+3);
        add_Q(c,b,(i-1)*3+3);
    }
}

int Find(int x){ return x!=father[x] ? father[x]=Find(father[x]) : father[x]; }

void tarjan(int u){
    color[u]=1;
    vis[u]=true;
    for(int i=headq[u];i!=-1;i=quary[i].next){
        int ID=quary[i].id;
        if(ans[ID]!=-1) continue;
        int v=quary[i].v;
        if(color[v]==0) continue;
        if(color[v]==1) ans[ID]=depth[u]-depth[v];
        if(color[v]==2) ans[ID]=depth[u]+depth[v]-2*depth[Find(v)];
    }
    for(int i=heade[u];i!=-1;i=graph[i].next){
        int vv=graph[i].v;
        int dis=graph[i].dis;
        if(!vis[vv]){
            depth[vv]=depth[u]+dis;
            tarjan(vv);
            color[vv]=2;
            father[vv]=u;
        }
    }
}

void solve(){
    tarjan(0);
    if(cases) printf("\n");
    for(int i=1;i<=q;i++){
        printf("%d\n",(ans[(i-1)*3+1]+ans[(i-1)*3+2]+ans[(i-1)*3+3])/2);
    }
    cases++;
}

int main(){
    while(scanf("%d",&n)!=EOF){
        init();
        input();
        solve();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 09:39:28

ZOJ_3195_Design the city(LCA+tarjan)的相关文章

HDU_2586 &amp;&amp; HDU_2874 (LCA+tarjan)

How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8541    Accepted Submission(s): 2997 Problem Description There are n houses in the village and some bidirectional roads connecting

最近公共祖先LCA(Tarjan算法)的思考和算法实现——转载自Vendetta Blogs

最近公共祖先LCA(Tarjan算法)的思考和算法实现 LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现 小广告:METO CODE 安溪一中信息学在线评测系统(OJ) //由于这是第一篇博客..有点瑕疵...比如我把false写成了flase...看的时候注意一下! //还有...这篇字比较多 比较杂....毕竟是第一次嘛 将就将就 后面会重新改!!! 首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖先节点,而最近公共祖先

POJ_1986_Distance Queries(LCA+tarjan)

Distance Queries Time Limit: 2000MS   Memory Limit: 30000KB   64bit IO Format: %I64d & %I64u Submit Status Description Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore w

hihoCoder #1067 : 最近公共祖先&#183;二 [ 离线LCA tarjan ]

传送门: #1067 : 最近公共祖先·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上上回说到,小Hi和小Ho用非常拙劣——或者说粗糙的手段山寨出了一个神奇的网站,这个网站可以计算出某两个人的所有共同祖先中辈分最低的一个是谁.远在美国的他们利用了一些奇妙的技术获得了国内许多人的相关信息,并且搭建了一个小小的网站来应付来自四面八方的请求. 但正如我们所能想象到的……这样一个简单的算法并不能支撑住非常大的访问量,所以摆在小Hi和小Ho面前的无非两种选择: 其一是

ZOJ Problem Set - 3195 Design the city 【Tarjan离线LCA】

题目:ZOJ Problem Set - 3195 Design the city 题意:给出一个图,求三点的连起来的距离. 分析:分别求出三点中任意两点的距离 / 2  = ans AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; #define N 50010 #define M 20010 struc

hdu-2874 Connections between cities(lca+tarjan+并查集)

题目链接: Connections between cities Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, s

【LCA/tarjan】POJ1470-Closest Common Ancestors

[题意] 给出一棵树和多组查询,求以每个节点为LCA的查询数有多少? [错误点] ①读入的时候,注意它的空格是随意的呀!一开始不知道怎么弄,后来看了DISCUSS区大神的话: 询问部分输入: scanf("%d",&m); for(int i=0;i<m;i++){ scanf(" (%d %d)",&a,&b); } 注意scanf(" 这里有一个空格 ②多组数据啊!注意这句话:The input file contents

最近公共祖先 LCA Tarjan算法

来自:http://www.cnblogs.com/ylfdrib/archive/2010/11/03/1867901.html 对于一棵有根树,就会有父亲结点,祖先结点,当然最近公共祖先就是这两个点所有的祖先结点中深度最大的一个结点. 0 | 1 /   \ 2      3 比如说在这里,如果0为根的话,那么1是2和3的父亲结点,0是1的父亲结点,0和1都是2和3的公共祖先结点,但是1才是最近的公共祖先结点,或者说1是2和3的所有祖先结点中距离根结点最远的祖先结点. 在求解最近公共祖先为问

HDU 2586 How far away ? (离线LCA Tarjan算法模板)

How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6422    Accepted Submission(s): 2411 Problem Description There are n houses in the village and some bidirectional roads connecting