• (树的直径)第九届湘潭市大学生程序设计比赛 H-Highway


    Highway

    Accepted : 25   Submit : 104
    Time Limit : 4000 MS   Memory Limit : 65536 KB 

    Highway

    In ICPCCamp there were n towns conveniently numbered with 1,2,,n connected with (n1) roads. The i-th road connecting towns ai and bi has length ci. It is guaranteed that any two cities reach each other using only roads.

    Bobo would like to build (n1) highways so that any two towns reach each using only highways. Building a highway between towns x and y costs him δ(x,y) cents, where δ(x,y) is the length of the shortest path between towns x and yusing roads.

    As Bobo is rich, he would like to find the most expensive way to build the (n1) highways.

    Input

    The input contains zero or more test cases and is terminated by end-of-file. For each test case:

    The first line contains an integer n. The i-th of the following (n1) lines contains three integers aibi and ci.

    • 1n105
    • 1ai,bin
    • 1ci108
    • The number of test cases does not exceed 10.

    Output

    For each test case, output an integer which denotes the result.

    Sample Input

    5
    1 2 2
    1 3 1
    2 4 2
    3 5 1
    5
    1 2 2
    1 4 1
    3 4 1
    4 5 2
    

    Sample Output

    19
    15

    长见识的一道题,第一次听说树的直径这一概念,百度了一下发现还是很强大的。

    对所有点,加上这一点到直径两端点的距离中大的一个即可,最后再减去一个直径(因为被重复算了一次)即可。

    (代码全是了解直径这一概念之后现写的,可能会比较繁琐)

    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <queue>
    #include <set>
    #include <map>
    #include <list>
    #include <stack>
    #define mp make_pair
    typedef long long ll;
    typedef unsigned long long ull;
    const int MAX=1e5+5;
    const int INF=1e9+5;
    const double M=4e18;
    using namespace std;
    const int MOD=1e9+7;
    typedef pair<int,int> pii;
    typedef pair<int,long long> pil;
    const double eps=0.000000001;
    int n;
    vector<pil> edge[MAX];
    int a,b;
    ll c;
    ll d[MAX];
    bool vi[MAX];
    int lo1,lo2;
    ll oh;
    int findlong(int st)
    {
        memset(vi,false,sizeof(vi));
        vi[st]=true;
        queue<pil> que;
        ll dismax=0,dis;
        int an,tem;
        que.push(mp(st,0LL));
        while(!que.empty())
        {
            tem=que.front().first;
            dis=que.front().second;
            pil lin;
            que.pop();
            for(int i=0;i<edge[tem].size();i++)
            {
                lin=edge[tem][i];
                if(!vi[lin.first])
                {
                    vi[lin.first]=true;
                    if(dismax<dis+lin.second)
                    {
                        dismax=dis+lin.second;
                        an=lin.first;
                    }
                    que.push(mp(lin.first,dis+lin.second));
                }
            }
        }
        oh=dismax;
        return an;
    }
    void dfs(int st)
    {
        memset(vi,false,sizeof(vi));
        vi[st]=true;
        queue<pil> que;
        int tem;
        ll dis;
        que.push(mp(st,0LL));
        while(!que.empty())
        {
            tem=que.front().first;
            dis=que.front().second;
            pil lin;
            que.pop();
            d[tem]=max(d[tem],dis);
            for(int i=0;i<edge[tem].size();i++)
            {
                lin=edge[tem][i];
                if(!vi[lin.first])
                {
                    vi[lin.first]=true;
                    que.push(mp(lin.first,dis+lin.second));
                }
            }
        }
    }
    ll finan;
    int main()
    {
        while(~scanf("%d",&n))
        {
            if(n==1)
            {printf("0
    ");continue;}
            oh=0LL;
            for(int i=1;i<=n;i++)
            {
                edge[i].clear();
                d[i]=0LL;
            }
            for(int i=1;i<n;i++)
            {
                scanf("%d%d%I64d",&a,&b,&c);
                edge[a].push_back(mp(b,c));
                edge[b].push_back(mp(a,c));
            }
            lo1=findlong(1);
            lo2=findlong(lo1);
            dfs(lo1);dfs(lo2);
            finan=0;
            for(int i=1;i<=n;i++)
            {
                finan+=d[i];
            }
            finan-=oh;
            cout<<finan<<"
    ";
        }
    
    }
  • 相关阅读:
    Tensorflow和pytorch安装(windows安装)
    KNN和K-Means算法
    numpy 介绍与使用
    opencv简单实用(cv2)
    使用matplotlib画图
    python图片处理PIL
    webpack
    Vue路由(vue-router)
    Vue组件
    Vue过滤器、生命周期函数和vue-resource
  • 原文地址:https://www.cnblogs.com/quintessence/p/6854429.html
Copyright © 2020-2023  润新知