2017 Xiangtan School Game H Highway longest path and

Source: Internet
Author: User

Highway

In Icpccamp there were n towns conveniently numbered with,..., N connected with (n−1) roads. The I-th road connecting towns AI and bi has length ci. It is guaranteed, that any, and cities reach each of the other using only roads.

Bobo would like-to-build (n−1) highways So, any of the 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 t owns X and y using roads.

As Bobo is rich, he would like to find the most expensive-to build the (n−1) highways.
Input

The input contains zero or more test cases and are 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, the output of 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

Test instructions is a hair like hdu2196.
The tree-shaped DP question contest was almost finished with the submission .... It's a pity that A has a silver medal.

#include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> using namespace
Std
const int maxn=100100;
    struct Node {int to;
    int next;
int Len;
}edge[maxn*2];//because there is no forward edge, so need twice times long long head[maxn];//head node long long tol; Long long maxn[maxn];//the maximum distance from the node down to the leaf long long smaxn[maxn];//large distance long long maxid[maxn];//the maximum distance corresponding to the ordinal long long SMAXID[MAXN
    ];//the number of times the large ordinal void init () {tol=0;
memset (head,-1,sizeof (head));
    } void Add (int a,int b,int len) {edge[tol].to=b;
    Edge[tol].len=len;
    Edge[tol].next=head[a];
    head[a]=tol++;
    Edge[tol].to=a;
    Edge[tol].len=len;
    EDGE[TOL].NEXT=HEAD[B];
head[b]=tol++;
    }//To find the maximum distance from the node v down to the leaf node//p is the Father node of V, void dfs1 (int u,int p) {maxn[u]=0;
    smaxn[u]=0;
        for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].to;
        if (v==p) continue;//can not look up Father node dfs1 (v,u);
        if (Smaxn[u]<maxn[v]+edge[i].len) {Smaxn[u]=maxn[v]+edge[i].len;    Smaxid[u]=v;
                if (Smaxn[u]>maxn[u]) {swap (smaxn[u],maxn[u]);
            Swap (Smaxid[u],maxid[u]);
        }}}}//p is the Father node of U, Len is the length of p to u void dfs2 (int u,int p) {for (int i=head[u];i!=-1;i=edge[i].next) {
        int v=edge[i].to;
        if (v==p) continue; if (V==maxid[u]) {if (Edge[i].len+smaxn[u]>smaxn[v]) {Smaxn[v]=edge[i]
                . Len+smaxn[u];
                Smaxid[v]=u;
                    if (Smaxn[v]>maxn[v]) {swap (smaxn[v],maxn[v]);
                Swap (smaxid[v],maxid[v]);
                }}} else {if (Edge[i].len+maxn[u]>smaxn[v]) {
                Smaxn[v]=edge[i].len+maxn[u];
                Smaxid[v]=u;
                    if (Smaxn[v]>maxn[v]) {swap (smaxn[v],maxn[v]); Swap (Maxid[v],smaxid[v]);
    }}} DFS2 (V,u);
    }} int main () {//freopen ("In.txt", "R", stdin);
    Freopen ("OUT.txt", "w", stdout);
    int n;
    int U,v,len;
        while (scanf ("%d", &n)!=eof) {init ();
            for (int i=2;i<=n;i++) {scanf ("%d%d%d", &u,&v,&len);
        Add (U,v,len);


        } DFS1 (1,-1);
        DFS2 (1,-1);
        Long Long sum=0;
        Long Long maxs=-1;
          for (int i=1;i<=n;i++) {sum+=maxn[i];
        Maxs=max (Maxs,maxn[i]);
    } cout<<sum-maxs<<endl;
} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.