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. 福建省队集训被虐记——DAY3

    昨天没写--今天补上吧 一如既往的跪了 棋盘 [问题描述] 给出一个N*M的方格棋盘,每个格子里有一盏灯和一个开关,开始的时候,所有的灯都是关着的.用(x, y)表示第x行,y列的格子.(x, y)的 ...

  2. MongoDB安装,打开及增,删,改,查

    MongoDB是目前最流行的NoSQl之一,NoSQL及No Only Sql,之所以这样叫我的理解是它摒弃了传统关系型数据库的字段类型的概念,而是将所有的数据以key-value方式存储,以key索 ...

  3. Thinkphp将中文年份转换为数字年份的问题

    今天遇到一个问题:想将中文年份转换为数字年份,例如:"二零一六"-->'2016'. 在网上搜了一下,没找到可以直接处理的函数(也许是我搜索信息的能力有限吧>_< ...

  4. 谷歌Cartographer学习(2)-原理阐述与源码解析

    最近终于写完了毕业论文.想仔细研究下Cartographer.无奈自己学识有限,先看下网上大牛的解析,作一个汇总. 一.泡泡机器人原创专栏-cartographer理论及实践浅析 http://mp. ...

  5. xib和Storyboard 创建Cell的方式

    xib 方式 .在Cell.h文件中加一个宏 #define cellIdentifier @"customCell" . ViewController中: - (void)vie ...

  6. Android编译过程详解(三)

    前面两节讲解了自定义Android编译项和创建Product产品配置文件,除了编译和定义产品相关环境变量外,还需要定义Board相关环境变量. 1. build/core/config.mk 109 ...

  7. NET中级课--文件,流,序列化2

    1.流的类型体系: 基础流    装饰器流    包装器类    帮助类 2.               stream file~     memory~     network~ stream是个 ...

  8. .Net+EF连接Oracle 10、11 开发,ODAC安装配置与使用详解

    Oracle数据访问组件 (ODAC)  全称:Oracle Data Access Components 优点:由于微软在.net framework4中会将System.Data.OracleCl ...

  9. HTML - Textarea - 空格的问题解决方式

    第一种方式: <textarea name="textareaname" rows="XX" cols="XX" ></t ...

  10. Margin是什么?

    Margin是什么 CSS 边距属性定义元素周围的空间.通过使用单独的属性,可以对上.右.下.左的外边距进行设置.也可以使用简写的外边距属性同时改变所有的外边距.——W3School 边界,元素周围生 ...