XTOJ1267 Highway 【最小生成树】

Highway

Accepted : 100      Submit : 336
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 y using 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.

1≤n≤105
1≤ai,bi≤n
1≤ci≤108
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

这OJ接受%I64d %lld会报WA!!!

模仿kurskal 把边从大到小依次加进生成树里即可
随便一个点出发dfs ,得到最远的一个点为x
从x出发dfs,得到最远的点为y
那dis[x][y]是树中距离最远的2点距离
(x,y)也是数的直径
同时,树中任意一点k,距离其他点的最远距离必定<=max(dis[k][x],dis[k][y])
因此 求出x,y的dfs,模拟kurskal即可

#include<stdio.h>
#include <iostream>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<deque>
#include<map>
#include<set>
#include<queue>
#include<math.h>
#include<string.h>
#include<string>

using namespace std;
#define ll long long
#define pii pair<int,int>

const int inf = 1e9 + 7;

const int N = 1e5+5;

struct Edge{
    int v,w,next;
}edge[2*N];
int head[N];

inline void addEdge(int k,int u,int v,int w){
    edge[k].v=v;
    edge[k].w=w;
    edge[k].next=head[u];
    head[u]=k;
}

ll dis1[N];
ll dis2[N];

void dfs(int u,int fa,ll*dis){
    for(int i=head[u];i!=-1;i=edge[i].next){
        const int&v=edge[i].v;
        const int&w=edge[i].w;
        if(v!=fa){
            dis[v]=dis[u]+w;
            dfs(v,u,dis);
        }
    }
}

pii init(int n){
    fill(dis1,dis1+n+1,0);
    dfs(1,-1,dis1);
    ll maxDis=0;
    int idx=0;
    for(int i=1;i<=n;++i){
        if(dis1[i]>maxDis){
            maxDis=dis1[i];
            idx=i;
        }
    }
    fill(dis1,dis1+n+1,0);
    dfs(idx,-1,dis1);
    int idx2=0;
    maxDis=0;
    for(int i=1;i<=n;++i){
        if(dis1[i]>maxDis){
            maxDis=dis1[i];
            idx2=i;
        }
    }
    fill(dis2,dis2+n+1,0);
    dfs(idx2,-1,dis2);
    return make_pair(idx,idx2);
}

ll slove(int n){
    if(n==1){
        return 0;
    }
    pii r=init(n);
    ll ans=dis1[r.second];
    for(int i=1;i<=n;++i){
        if(i==r.first||i==r.second){
            continue;
        }
        ans+=max(dis1[i],dis2[i]);
    }
    return ans;
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    int n;
    while(~scanf("%d",&n)){
        fill(head,head+n+1,-1);
        for(int i=0;i<n-1;++i){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            addEdge(2*i,a,b,c);
            addEdge(2*i+1,b,a,c);
        }
        printf("%I64d\n",slove(n));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值