zoukankan      html  css  js  c++  java
  • zoj 3195 Design the city

    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<map>
    #include<set>
    #include<stack>
    #include<queue>
    #include<cmath>
    #include<vector>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #define  inf 0x0f0f0f0f
    using namespace std;
    
    const int maxn=100000+10;
    struct node
    {
        int v,c;
    };
    
    vector<node>tree[maxn],que[maxn];
    int father[maxn],num[maxn*3],dis[maxn];
    bool vis[maxn],mark[maxn];
    
    void init(int n)
    {
        for (int i=0;i<=n;i++)
        {
            tree[i].clear();
            que[i].clear();
            father[i]=i;
            vis[i]=0;
            mark[i]=0;
            dis[i]=0;
        }
    }
    
    int find(int x)
    {
        return x==father[x]?x:father[x]=find(father[x]);
    }
    
    void Lca(int u)
    {
        father[u]=u;
        for (int i=0;i<tree[u].size();i++)
        {
            int v=tree[u][i].v;
            if (!mark[v])
            {
                mark[v]=1;
                dis[v]=dis[u]+tree[u][i].c;
                Lca(v);
                father[v]=u;
            }
        }
        vis[u]=true;
        for (int i=0;i<que[u].size();i++)
        {
            int v=que[u][i].v;
            if (vis[v])
            {
                num[que[u][i].c]=dis[u]+dis[v]-2*dis[find(v)];
            }
        }
    }
    
    int main()
    {
        int n,q,x,y,c,ff=0;
        while (scanf("%d",&n)!=EOF)
        {
            if(ff) printf("
    ");
            ff=1;
            init(n);
            for (int i=1;i<n;i++)
            {
                scanf("%d%d%d",&x,&y,&c);
                node temp;
                temp.v=y; temp.c=c;
                tree[x].push_back(temp);
                temp.v=x;
                tree[y].push_back(temp);
            }
            scanf("%d",&q);
            for (int i=0;i<q;i++)
            {
                int L=i*3;
                scanf("%d%d%d",&x,&y,&c);
                node temp;
                temp.v=y; temp.c=L;
                que[x].push_back(temp);
                temp.v=x;
                que[y].push_back(temp);
    
                temp.v=c; temp.c=L+1;
                que[x].push_back(temp);
                temp.v=x;
                que[c].push_back(temp);
    
                temp.v=c; temp.c=L+2;
                que[y].push_back(temp);
                temp.v=y;
                que[c].push_back(temp);
            }
            memset(num,0,sizeof(num));
            mark[0]=1;
            Lca(0);
            for (int i=0;i<q*3;i+=3)
            printf("%d
    ",(num[i]+num[i+1]+num[i+2])/2);
        }
        return 0;
    }

    作者 chensunrise

    至少做到我努力了
  • 相关阅读:
    使用某些 DOCTYPE 时会导致 document.body.scrollTop 失效
    VB.NET 笔记1
    知识管理系统Data Solution研发日记之一 场景设计与需求列出
    知识管理系统Data Solution研发日记之五 网页下载,转换,导入
    折腾了这么多年的.NET开发,也只学会了这么几招 软件开发不是生活的全部,但是好的生活全靠它了
    分享制作精良的知识管理系统 博客园博客备份程序 Site Rebuild
    知识管理系统Data Solution研发日记之四 片段式数据解决方案
    知识管理系统Data Solution研发日记之二 应用程序系列
    知识管理系统Data Solution研发日记之七 源代码与解决方案
    知识管理系统Data Solution研发日记之三 文档解决方案
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3750751.html
Copyright © 2011-2022 走看看