UESTC_Frozen Rose-Heads CDOJ 791
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的更多相关文章
- cdoj 791 Frozen Rose-Heads
//本来想做白书上一题 结果发现又要二染色 又要dp的 想了两个小时没想通 然后做了个傻逼题安慰自己 解:不多说,就是递归到叶节点,然后回来的时候在解决子树和直接删边的代价中间取个最小值 #inclu ...
- 用rose画UML图(用例图,活动图)
用rose画UML图(用例图,活动图) 首先,安装rose2003,电脑从win8升到win10以后,发现win10并不支持rose2003的安装,换了rose2007以后,发现也不可以. 解决途径: ...
- 关于refs/for/ 和refs/heads/
1. 这个不是git的规则,而是gerrit的规则, 2. Branches, remote-tracking branches, and tags等等都是对commite的引用(re ...
- 解决Rational Rose 中 没有 Data modeler 选项的问题
在 Rose 没有 Data modeler 选项的原因是没有将 Data modeler 这块功能勾选上. 解决方案: 菜单栏--Add-Ins--Add-Ins Manager-->找到 ...
- Rational Rose
Rational Rose简明实用教程 http://furzoom.com/rational-rose-course/ Unidirectional Association 单向关联 general ...
- 带领大家安装Rational rose
Rational rose下载地址:http://pan.baidu.com/s/1hqIIyJq?qq-pf-to=pcqq.c2c 下载好后,文件里有4个安装文档: 解压CD1和CD2到当前目录, ...
- UML基础与Rose建模实训教程
目 录 第1章 初识UML. 1 1.1 初识UML用例图... 1 1.2 初识UML类图... 3 第2章 Rational Rose工具... 6 2.1 安装与配置Rational Ro ...
- Rational.Rose.Enterprise.v7.0 (2007)安装分享
很多人都在找rational软件,很多都是2003的,有的宣称是2007,但结果还是2003.也许真的不存在Rational.Rose 2007,不过有IBM.Rational.Rose.Enterp ...
- Rational Rose :从用例图开始
前置条件:安装Rational Rose 2003 找开Rose工具,选择用例视图 Use Case View 先看看这个视图下面都有哪些工具,都能做一些什么: 下面详细说一下: 用例视图下面有工具 ...
随机推荐
- jquery datepicker日期控件用法
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.c ...
- Palindrome Partitioning 解答
Question Given a string s, partition s such that every substring of the partition is a palindrome. R ...
- Hdu2437-Jerboas(取余数判重搜索)
Jerboas are small desert-living animals, which resemble mice with a long tufted tail and very long h ...
- awk笔记1
grep: 文本过滤器 grep 'pattern' input_file ... sed:流编辑器 awk: 报告生成器 格式化以后,显示 AWK a.k.a. Aho, Kernigh ...
- Unity PlayerPrefs类进行扩展(整个对象进行保存)
盘子脸在制作单机游戏的时候,先以为没有好多数据需要保存本地. 就没有使用json等格式自己进行保存. 使用PlayerPrefs类,但是后面字段越来越多的时候. PlayerPrefs保存就发现要手动 ...
- Unity编辑器-创建单独编辑框,折叠框,提示框
今天我们就来学习如何创建一个编辑框,上面绘制一个折叠框里面有四种消息框. 代码如下: using UnityEngine; using System.Collections; using UnityE ...
- python - 文件
''' 模式 描述 r 以读方式打开文件,可读取文件信息. w 以写方式打开文件,可向文件写入信息.如文件存在,则清空该文件,再写入新内容 a 以追加模式打开文件(即一打开文件,文件指针自动移到文件末 ...
- 详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON() —(转)
一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表(是可选的,也可以将要传的参数写在url里面),callback为请求成功后的回调函数,该 ...
- VBA清除Excelpassword保护,2003/2007/2010均适用
Sub Macro1() ' ' Breaks worksheet and workbook structure passwords. Jason S ' probably originator of ...
- drwtsn32.exe 遇到问题须要关闭。我们对此引起的不便表示抱歉
我的机器老是这样.启动起来就有这个... 那位高手能告诉我这是怎么会事.故障的原因以及解决的办法. 最佳答案 drwtsn32.exe是windows的一项磁盘检查程序,同一时候也是鸡肋程序,最好的办 ...