zoukankan      html  css  js  c++  java
  • xtu summer individual 1 C

    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算法


     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #define LL long long
    13 #define INF 0x3f3f3f3f
    14 using namespace std;
    15 const int maxv = 50010;
    16 const int maxn = 70010;
    17 struct arc{
    18     int to,w;
    19 };
    20 struct Q{
    21     int index,v;
    22 };
    23 vector<arc>g[maxv];
    24 vector<Q>qy[maxn];
    25 int ans[maxn],uf[maxv],dis[maxv],n,m;
    26 bool vis[maxv];
    27 int _find(int x){
    28     if(uf[x] != x)
    29         uf[x] = _find(uf[x]);
    30     return uf[x];
    31 }
    32 void dfs(int u){
    33     vis[u] = true;
    34     uf[u] = u;
    35     int i,j,k,v;
    36     for(i = 0; i < qy[u].size(); i++){
    37         v = qy[u][i].v;
    38         if(vis[v])
    39             ans[qy[u][i].index] += dis[u]+dis[v]-2*dis[_find(v)];
    40     }
    41     for(i = 0; i < g[u].size(); i++){
    42         v = g[u][i].to;
    43         if(!vis[v]){
    44             dis[v] = dis[u]+g[u][i].w;
    45             dfs(v);
    46             uf[v] = u;
    47         }
    48     }
    49 }
    50 int main(){
    51     int i,j,u,v,w,t = 0;
    52     while(~scanf("%d",&n)){
    53         if(t) printf("
    ");
    54         for(i = 0; i <= n; i++){
    55             g[i].clear();
    56             qy[i].clear();
    57             uf[i] = i;
    58             vis[i] = false;
    59         }
    60         for(i = 1; i < n; i++){
    61             scanf("%d%d%d",&u,&v,&w);
    62             g[u].push_back((arc){v,w});
    63             g[v].push_back((arc){u,w});
    64         }
    65         scanf("%d",&m);
    66         for(i = 1; i <= m; i++){
    67             scanf("%d %d %d",&u,&v,&w);
    68             qy[u].push_back((Q){i,v});
    69             qy[v].push_back((Q){i,u});
    70             qy[u].push_back((Q){i,w});
    71             qy[w].push_back((Q){i,u});
    72             qy[v].push_back((Q){i,w});
    73             qy[w].push_back((Q){i,v});
    74         }
    75         memset(ans,0,sizeof(ans));
    76         dis[0] = 0;
    77         dfs(0);
    78         for(i = 1; i <= m; i++)
    79             printf("%d
    ",ans[i]>>1);
    80         t++;
    81     }
    82     return 0;
    83 }
    View Code
  • 相关阅读:
    VFP正则表达式判断是否是手机号码/电子邮件
    VFP自定义函数StringFormat (仿.NET String.Format 方法)
    解决SOAPCLIENT访问WebSerivce外网发布端口
    VFP调用SOAPTOOLKIT 低级API
    经典的Hello World VFP前端调后端C# Webservice
    VFP不同句柄 同一事务处理 统一提交或回滚
    C++文件(夹)选择对话框操作
    Linxu之rz和sz命令
    测试标题
    自定义Silverlight TextBox 具有下拉框提示控件
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3884144.html
Copyright © 2011-2022 走看看