zoukankan      html  css  js  c++  java
  • FZU 2195 检查站点

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

    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
    ", summ - ff);
        }
        return 0;
    }
  • 相关阅读:
    WebApi整合Unity容器实现IOC编程
    用户登录
    Mvc验证码
    代理模式 实现aop
    装饰器模式,实现aop
    Redis
    图片缩放
    递归操作文件
    几种文件的读写方式
    C#WebApi自动生成文档
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4503307.html
Copyright © 2011-2022 走看看