1. 程式人生 > >(樹的直徑)第九屆湘潭市大學生程序設計比賽 H-Highway

(樹的直徑)第九屆湘潭市大學生程序設計比賽 H-Highway

frame rst ould inf tin 中大 nat amp +=

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 (n?1) 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 (n?1) 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 (n?1)

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 (n?1) 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

長見識的一道題,第一次聽說樹的直徑這一概念,百度了一下發現還是很強大的。

對所有點,加上這一點到直徑兩端點的距離中大的一個即可,最後再減去一個直徑(因為被重復算了一次)即可。

(代碼全是了解直徑這一概念之後現寫的,可能會比較繁瑣)

#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\n");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<<"\n";
    }

}

(樹的直徑)第九屆湘潭市大學生程序設計比賽 H-Highway