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 先看看这个视图下面都有哪些工具,都能做一些什么: 下面详细说一下: 用例视图下面有工具 ...
随机推荐
- ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase
原文地址:http://www.51csharp.com/MVC/882.html ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL 引言-- 在初级篇中,我们 ...
- UESTC_方老师和农场 2015 UESTC Training for Graph Theory<Problem L>
L - 方老师和农场 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- 关于Tcp三次握手的思考
一.为什么不能使两次握手,两次握手就应该可以保证线路的畅通? 1) 只能建立一个方向的连接,称为半连接 记住TCP是全双工的. A向B发出请求,同时收到B的确认,这时只有A.B知道A到B的连接成功了. ...
- HTTP请求&&响应
在视频上截的图....俗话说好记性不如烂笔头,所以就保留下来 请求: 响应: 状态码: 请求头和响应头的解释:
- 脚本控制向Android模拟拨打电话,发送短信,定位设置功能
做行为触发的时候要向模拟器实现拨打电话,发送短信,定位设置的的功能,可以很方便通过telnet localhost 5554实现. 写个脚本很快的搞定了.网上资料很多,脚本的很少,也所积点德啦. 写 ...
- UVA 10285 Longest Run on a Snowboard(记忆化搜索)
Problem C Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 sec ...
- android中创建模拟器的 SDCard
在eclipse中安装了android环境后,可以直接创建AVD和sdcard的,windows->Android Virtual Device Manager,创建一个AVD时,可以同时创建s ...
- poj 3045 Cow Acrobats(二分搜索?)
Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...
- Hibernate框架后续
持久化对象的唯一标识OID 1:我们都知道,在java中按照内存地址来区分同一个类的不同对象 而关系数据库按照主键来区分一条记录 在Hibernate中使用OID来建立内存中的对象和数据 ...
- Hadoop 写SequenceFile文件 源代码
package com.tdxx.hadoop.sequencefile; import java.io.IOException; import org.apache.hadoop.conf.Conf ...