首页 > 其他 > 详细

FZU 2195 检查站点

时间:2015-05-14 15:52:41      阅读:157      评论:0      收藏:0      [点我收藏+]

求出根节点到每个叶子节点的距离,找到最大的。然后总权值减去最大叶子距离就是答案。

GNU C++ AC

Visual C++  TLE

 

#include<stdio.h>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

const int maxn = 111111;
vector<int> ljb[maxn];
int cost[maxn], ff;

void dfs(int node, int fahther, int tot)
{
    if (tot > ff) ff = tot;
    int i;
    for (i = 0; i < ljb[node].size(); i++)
        dfs(ljb[node][i], node, tot + cost[ljb[node][i]]);
}
int main()
{
    int u, v, c, n, i;
    while (~scanf("%d", &n))
    {
        ff = 0;
        for (i = 0; i <= n; i++) ljb[i].clear();
        int summ = 0;
        cost[1] = 0; ljb[0].push_back(1);
        for (i = 0; i < n - 1; i++)
        {
            scanf("%d%d%d", &u, &v, &c);
            ljb[u].push_back(v);
            cost[v] = c;
            summ = summ + c;
        }
        dfs(1, 0, 0);
        printf("%d\n", summ - ff);
    }
    return 0;
}

 

FZU 2195 检查站点

原文:http://www.cnblogs.com/zufezzt/p/4503307.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!