• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 18,  comments - 5,  trackbacks - 0

            一、題目描述

            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
            


            二、分析
                  用RMQ解決的LCA問題,詳細算法:LCA問題
            三、代碼

              1#include<iostream>
              2#include<cmath>
              3#include<list>
              4using namespace std;
              5int n, q;
              6struct node
              7{
              8    int lab, dis;
              9    void init(int l, int d)
             10    {
             11        lab = l; dis = d;
             12    }

             13}
            ;
             14int v1, v2, v3, len;
             15list<node> g[50001];
             16int ei, e[100002], r[50001], l[100002], d[50001];
             17bool visit[50001];
             18int pow2[18];
             19int mmin[18][100002];
             20void dfs(int u, int dep)
             21{
             22    e[++ei] = u; l[ei] = dep;
             23    if(visit[u]) return;
             24    visit[u] = true;
             25    list<node>::iterator it = g[u].begin();
             26    while(it != g[u].end())
             27    {
             28        int v = it->lab, len = it->dis;
             29        if(!visit[v])
             30        {
             31            d[v] = min(d[v], d[u] + len);
             32            dfs(v, dep+1);
             33            e[++ei] = u; l[ei] = dep;
             34            
             35        }

             36        it++;
             37    }

             38}

             39void init_rmq()
             40{
             41    ei = 0;
             42    memset(visit, 0sizeof(visit));
             43    d[0= 0;
             44    dfs(01);
             45    memset(r, -1sizeof(r));
             46    for(int i=1; i<=ei; i++)
             47        if(r[e[i]] == -1)
             48            r[e[i]] = i;
             49    memset(mmin, 0sizeof(mmin));
             50    for(int i=1; i<=ei; i++)
             51        mmin[0][i] = i;
             52    int t1 = (int)(log((double)ei) / log(2.0));
             53    for(int i=1; i<=t1; i++)
             54        for(int j=1; j + pow2[i] - 1<=ei; j++)
             55        {
             56            int a = mmin[i-1][j], b = mmin[i-1][j+pow2[i-1]];
             57            if(l[a] <= l[b])
             58                mmin[i][j] = a;
             59            else
             60                mmin[i][j] = b;
             61        }

             62}

             63int rmq(int u, int v)
             64{
             65    int i = r[u], j = r[v];
             66    if(i > j) swap(i, j);
             67    int t1 = (int)(log((double)j - i + 1/ log(2.0));
             68    int a = mmin[t1][i], b = mmin[t1][j - pow2[t1] + 1];
             69    if(l[a] <= l[b])
             70        return e[a];
             71    else
             72        return e[b];
             73}

             74int main()
             75{
             76    for(int i=0; i<18; i++)
             77        pow2[i] = 1 << i;
             78    bool flag = false;
             79    while(scanf("%d"&n) != EOF)
             80    {
             81        if(flag) printf("\n");
             82        flag = true;
             83        for(int i=0; i<n; i++)
             84        {
             85            g[i].clear();
             86            d[i] = INT_MAX;
             87        }

             88        for(int i=0; i<n-1; i++)
             89        {
             90            scanf("%d%d%d"&v1, &v2, &len);
             91            node n1; n1.init(v2, len);
             92            g[v1].push_back(n1);
             93            node n2; n2.init(v1, len);
             94            g[v2].push_back(n2);
             95        }

             96        init_rmq();
             97        scanf("%d"&q);
             98        while(q--)
             99        {
            100            int res = INT_MAX;
            101            scanf("%d%d%d"&v1, &v2, &v3);
            102            int temp = 0;
            103            int lca1 = rmq(v1, v2);
            104            temp = d[v1] + d[v2] - 2*d[lca1];
            105            int lca2 = rmq(lca1, v3);
            106            temp += d[lca1] + d[v3] - 2*d[lca2];
            107            res = min(res, temp);
            108            temp = 0;
            109            lca1 = rmq(v1, v3);
            110            temp = d[v1] + d[v3] - 2*d[lca1];
            111            lca2 = rmq(lca1, v2);
            112            temp += d[v2] + d[lca1] - 2*d[lca2];
            113            res = min(res, temp);
            114            temp = 0;
            115            lca1 = rmq(v2, v3);
            116            temp = d[v2] + d[v3] - 2*d[lca1];
            117            lca2 = rmq(lca1, v1);
            118            temp += d[v1] + d[lca1] - 2*d[lca2];
            119            res = min(res, temp);
            120            printf("%d\n", res);
            121        }

            122    }

            123}
            posted on 2009-07-02 20:55 Icyflame 閱讀(1269) 評論(0)  編輯 收藏 引用 所屬分類: 解題報告
            91精品国产9l久久久久| 久久亚洲精品无码AV红樱桃| 国产高清美女一级a毛片久久w | 免费精品国产日韩热久久| 一本久久精品一区二区| 久久精品国产精品亚洲精品| 97久久精品无码一区二区| 国内精品伊人久久久久网站| 亚洲国产精品无码久久一区二区| 97久久精品国产精品青草| 久久久这里有精品中文字幕| 久久亚洲日韩看片无码| 久久久久久综合一区中文字幕 | 久久精品国产乱子伦| 久久久青草青青亚洲国产免观| 欧美久久久久久午夜精品| 国内精品久久久久影院优| 久久久午夜精品福利内容| 91精品国产91久久久久久蜜臀| 欧美丰满熟妇BBB久久久| 日本亚洲色大成网站WWW久久| 久久福利青草精品资源站| 无码超乳爆乳中文字幕久久| 伊人久久大香线蕉成人| 国产成人久久精品麻豆一区 | 久久久久99精品成人片牛牛影视| 久久久无码精品亚洲日韩蜜臀浪潮 | 18岁日韩内射颜射午夜久久成人| 久久人人爽人人爽人人AV东京热| 中文字幕精品无码久久久久久3D日动漫 | 久久久久久久综合日本亚洲 | 精产国品久久一二三产区区别| 国产成人无码精品久久久免费| 91精品国产色综合久久| 无码国产69精品久久久久网站| 午夜天堂av天堂久久久| 人妻精品久久久久中文字幕一冢本| 久久国产AVJUST麻豆| 久久综合亚洲色一区二区三区| 欧美黑人激情性久久| 无码伊人66久久大杳蕉网站谷歌|