The winter is coming and all the experts are warning that it will be the coldest one in the last hundred years. Freddy needs to make sure that his garden does not sustain any damage. One of the most important tasks is to make sure that no water remains in his large watering system.

All the water comes from a central node and is distributed by pipes to neighboring nodes and soon. Each node is either a sprinkler (rose head) with no outgoing pipe or an internal node with one or more outgoing pipes leading to some other nodes. Every node has exactly one incoming pipe, except for the central node which takes the water directly from a well and has no incoming pipe. Every pipe has a valve that stops all the water going through the pipe. The valves are of different quality and age, so some may be harder to close than others.

Freddy knows his valves well and has assigned a value to each pipe representing the amount of effort needed to close the corresponding valve. He asks you to help him count the minimum effort needed to close some valves so that no water goes to the sprinklers.

Input

The input contains several test cases. Each test case starts with a line with two integers, the number of nodes n (2≤n≤1,000), and the number of the central node c (1≤c≤n). Each of the next n−1 lines represents one pipe and contains three integers, u, v (1≤u,v≤n) and w(1≤w≤1,000), where u and v are the nodes connected by a pipe and w is the effort needed to close the valve on that pipe. You may assume that every node is reachable from the central node.

Output

For each test case, output a single line containing the minimum sum of efforts of valves to be closed, such that the central node gets separated from all sprinklers.

解题报告

基础树形DP。。没啥好讲的

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <set>
using namespace std;
const int maxn = + ;
const int INF = << ;
set<int>E[maxn];
int n,c,cost[maxn][maxn],pre[maxn],dp[maxn]; void Delete_n(int cur,int fat)
{
pre[cur] = fat;
for(set<int>::iterator it = E[cur].begin() ; it != E[cur].end() ; ++ it)
{
int tar = *it;
if (*it != fat)
Delete_n(tar,cur);
}
if (fat != -)
E[cur].erase(fat);
} int slove(int cur)
{
if (dp[cur] != -)
return dp[cur];
int & ans = dp[cur] = INF;
if (!E[cur].size())
return ans = cost[cur][pre[cur]];
int res = ;
for(set<int>::iterator it = E[cur].begin() ; it != E[cur].end() ; ++ it)
{
res += slove(*it);
}
if (cur == c)
ans = res;
else
ans = min(res,cost[cur][pre[cur]]);
return ans;
} int main(int argc,char * argv[])
{
while(scanf("%d%d",&n,&c) == )
{
c--;
memset(dp,-,sizeof(dp));
for(int i = ; i < n- ; ++ i)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
E[u-].insert(v-);
E[v-].insert(u-);
cost[u-][v-] = cost[v-][u-] = w;
}
Delete_n(c,-);
slove(c);
cout << dp[c] << endl;
for(int i = ; i < n ; ++ i)
E[i].clear();
}
return ;
}

UESTC_Frozen Rose-Heads CDOJ 791的更多相关文章

  1. cdoj 791 Frozen Rose-Heads

    //本来想做白书上一题 结果发现又要二染色 又要dp的 想了两个小时没想通 然后做了个傻逼题安慰自己 解:不多说,就是递归到叶节点,然后回来的时候在解决子树和直接删边的代价中间取个最小值 #inclu ...

  2. 用rose画UML图(用例图,活动图)

    用rose画UML图(用例图,活动图) 首先,安装rose2003,电脑从win8升到win10以后,发现win10并不支持rose2003的安装,换了rose2007以后,发现也不可以. 解决途径: ...

  3. 关于refs/for/ 和refs/heads/

    1.     这个不是git的规则,而是gerrit的规则, 2.     Branches, remote-tracking branches, and tags等等都是对commite的引用(re ...

  4. 解决Rational Rose 中 没有 Data modeler 选项的问题

    在  Rose  没有 Data modeler 选项的原因是没有将 Data modeler 这块功能勾选上. 解决方案: 菜单栏--Add-Ins--Add-Ins Manager-->找到 ...

  5. Rational Rose

    Rational Rose简明实用教程 http://furzoom.com/rational-rose-course/ Unidirectional Association 单向关联 general ...

  6. 带领大家安装Rational rose

    Rational rose下载地址:http://pan.baidu.com/s/1hqIIyJq?qq-pf-to=pcqq.c2c 下载好后,文件里有4个安装文档: 解压CD1和CD2到当前目录, ...

  7. UML基础与Rose建模实训教程

    目  录 第1章  初识UML. 1 1.1 初识UML用例图... 1 1.2 初识UML类图... 3 第2章  Rational Rose工具... 6 2.1 安装与配置Rational Ro ...

  8. Rational.Rose.Enterprise.v7.0 (2007)安装分享

    很多人都在找rational软件,很多都是2003的,有的宣称是2007,但结果还是2003.也许真的不存在Rational.Rose 2007,不过有IBM.Rational.Rose.Enterp ...

  9. Rational Rose :从用例图开始

    前置条件:安装Rational Rose 2003 找开Rose工具,选择用例视图  Use Case View 先看看这个视图下面都有哪些工具,都能做一些什么: 下面详细说一下: 用例视图下面有工具 ...

随机推荐

  1. NLog 2.0.0.2000 使用实例

    原文地址:http://www.cnblogs.com/sorex/archive/2013/01/31/2887174.html ---------------------------------- ...

  2. Atitit.软件guibuttonand面板---os区-----linux windows搜索文件 目录

    Atitit.软件guibuttonand面板---os区-----搜索文件 1. Find 1 2. 寻找文件夹 1 3. 2. Locate// everything 1 4. 3. Wherei ...

  3. (HYSBZ)BZOJ 1588 营业额统计

    营业额统计 Time Limit: 5000MS   Memory Limit: 165888KB   64bit IO Format: %lld & %llu Description 营业额 ...

  4. HDU4907小技巧

    原题http://acm.hdu.edu.cn/showproblem.php?pid=4907 Task schedule Time Limit: 2000/1000 MS (Java/Others ...

  5. vim的正则表达式(二)应用实例

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  6. 微信小程序demo豆瓣图书

    最近微信小程序被炒得很火热,本人也抱着试一试的态度下载了微信web开发者工具,开发工具比较简洁,功能相对比较少,个性化设置也没有.了解完开发工具之后,顺便看了一下小程序的官方开发文档,大概了解了小程序 ...

  7. Winform开发之窗体传值

    Winform的窗体之间的数据传递是开发的必备技术,下面介绍几种典型的传值方法 1.构造函数传值,但这种方法是单向的(推荐) 上代码,先传值 private void button2_Click(ob ...

  8. iOS nav加角标

    写一个类别加上就可以啦 #import "UIBarButtonItem+Badge.h" #import "BadgeView.h" #import < ...

  9. C#操作Excel总结

    0. 导入命名空间:  1 2 3 4 using Microsoft.Office.Core; using Microsoft.Office.Interop.Excel; using System. ...

  10. (转) 谈C/C++指针精髓

    原文:http://www.cnblogs.com/madengwei/archive/2008/02/18/1072410.html   [摘要]    指针是C和C++语言编程中最重要的概念之一, ...