2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛(湖南) 暨 第九届湘潭市大学生程序设计比赛H.Highway(树的直径)



Highway

Accepted : 122 Submit : 393
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  y  using 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  ai bi  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

Source

XTU OnlineJudge
题目大意:给n个点,有n-1条边,要求给出所有点的最长路之和再减去两点间最长路
解题思路:在比赛的时候,想到用树的直径,因为你希望能够尽可能的找到每个点的最长路,意味着,你要联系树的直径,在直径上做文章,由此可知,但是时间来不及,加上没有想通细节上的问题,这道题没有出,一开始想的是找到树的直径的两个点后,开始判断其他点是否在直径上,然后在计算不在直径上的点的最长路上出现了一点问题,最后意识到,当你找到树的直径后,意味着你从直径两点都BFS一边,对于每个点取一个max即可,不用不断的判
#include<iostream>    
#include<cstdio>  
#include<stdio.h>  
#include<cstring>    
#include<cstdio>    
#include<climits>    
#include<cmath>   
#include<vector>  
#include <bitset>  
#include<algorithm>    
#include <queue>  
#include<map>  
using namespace std; 

vector<int>tree[100003];
int t,f;
vector<long long int>lu[100003];
long long int ma,l,sum;
long long int check[100003];
long long int dis[100003],a[100003];
int n;

int bfs(int x)
{
    int i,k,ans;
    ma=0;
    memset(dis,0,sizeof(dis));
    memset(check,0,sizeof(check));
    queue<int> qua;
    qua.push(x);
    dis[x]=0;
    check[x]=x;
    while(!qua.empty())
    {
        //cout<<11111111111111<<endl;
        k=qua.front();
        qua.pop();
        for(i=0;i<tree[k].size();i++)
        {
            int ss=tree[k][i];
            if(check[ss]==0)
            {
                check[ss]=k;
                qua.push(ss);
                dis[ss]=dis[k]+lu[k][i];
                if(ma<dis[ss])
                {
                //    cout<<"ss"<<ss<<endl;
                    ans=ss;
                    ma=dis[ss];
                }
            }
        }
    }
    return ans;
}
int main()
{
    int i,x,y,k;
    while(~scanf("%d", &n))
    {
        memset(tree,0,sizeof(tree));
        memset(lu,0,sizeof(lu));
        memset(check,0,sizeof(check));
        for(i=1;i<n;i++)
        {
            scanf("%d%d%I64d", &x, &y, &l);
            tree[x].push_back(y);
            lu[x].push_back(l);
            tree[y].push_back(x);
            lu[y].push_back(l);
        }
        t=f=0;
        t=bfs(1);
       // cout<<"---------------"<<endl;
        f=bfs(t);
        //cout<<t<<" "<<f<<endl;
        memset(a,0,sizeof(a));
        for(i=1;i<=n;i++)
        {
           a[i]=max(dis[i],a[i]);
        }
        sum=0;
        bfs(f);
        for(i=1;i<=n;i++)
        {
            a[i]=max(a[i],dis[i]);
            sum+=a[i];
        }
        sum-=ma;
        printf("%I64d\n", sum);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值