码迷,mamicode.com
首页 > 其他好文 > 详细

清北 游

时间:2017-05-21 10:53:17      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:head   turn   for   log   UI   define   its   power   分析   

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<climits>
#include<string>
#include<cstdlib>
#include<ctime>
#define MAX 1000000007
#define LL long long
using namespace std;

int tot,num[150005],power[150005],nxt[150005],head[150005],n,ans,all,dep[150005],vis[150005],d; 

void add(int p1,int p2,int wi)
{
    tot++;
    num[tot]=p2;
    power[tot]=wi;
    nxt[tot]=head[p1];
    head[p1]=tot;
}

void dfs(int now,int z)
{
    int i,p=0;
    
    for(i=head[now];i;i=nxt[i])
    {
        if(!vis[num[i]]) 
        {
            p=1;
            vis[num[i]]=1;        
            dfs(num[i],z+power[i]);        
        }            
    }
    
    if(p==0)
    {
        d=max(d,z);
        
        return;
    }
    
}


int main()
{    
    int s,t,w,i;

    scanf("%d",&n);
    
    for(i=1;i<=n-1;i++)
    {
        scanf("%d%d%d",&s,&t,&w);
        add(s,t,w);
        add(t,s,w);
        
        all+=w;
    }
    
    all=all*2;
    
    vis[1]=1;
    dfs(1,0);
    ans=all-d;
    
    printf("%d",ans);
    
    return 0;
}

  很容易分析得,最短=2*总和-最长链

 

清北 游

标签:head   turn   for   log   UI   define   its   power   分析   

原文地址:http://www.cnblogs.com/wspl98765/p/6883996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!