福州大学第十二届程序设计竞赛-F - 检查站点

F - 检查站点
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

在山上一共有N个站点需要检查,检查员从山顶出发去各个站点进行检查,各个站点间有且仅有一条通路,检查员下山前往站点时比较轻松,而上山时却需要额外的时间,问最后检查员检查完所有站点时所需要的额外时间最少是多少。

Input

包含多组数据 每组数据输入第一行为一个整数N 表示站点个数(1<=N<=100000),接下去N-1 行 每行3个整数 x,y,z(1<=z<=10000) 检查站x为检查站y的父节点,x,y之间有一条通路,从y到x需要额外z的时间。(父节点在子节点上方,山顶固定标号为1)

Output

输出一行一个整数表示最少需要花费的额外时间。

Sample Input

61 2 12 4 11 3 13 5 13 6 1

Sample Output

3


思路:遍历所有路径,根到叶子的最长路劲

#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
using namespace std;


const int mm = 100000+50;
struct node {
	int f;
	long long  v_all;
	int c_num;
	long long  length;
}p[mm];

queue<node> q;

long long dfs(int i, long long count){
	while(i){
		count+=p[i].length;
		i=p[i].f;
	}
	return count;
}
	
int main(){
	int n;
	int f,c,l;
	while(~scanf("%d",&n)){
		memset(p,0,sizeof(p));
		long long sum = 0;
		long long _max = 0;
		for(int i = 0; i < n-1; i++){
			scanf("%d%d%d",&f,&c,&l);
			p[c].f = f; p[c].length = l;
			p[f].c_num++;
			sum+=l;
		}
		for(int i = 2; i< n; i++)
			if(!p[i].c_num)
				_max = max(_max,dfs(i,0));
		
		printf("%I64d\n", sum-_max);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值