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 先看看这个视图下面都有哪些工具,都能做一些什么: 下面详细说一下: 用例视图下面有工具 ...
随机推荐
- The Hitchhiker’s Guide to Python! — The Hitchhiker's Guide to Python
The Hitchhiker's Guide to Python! - The Hitchhiker's Guide to Python The Hitchhiker's Guide to Pytho ...
- ios相册
1, 系统图片剪裁的问题 http://www.cnblogs.com/liulunet/archive/2013/01/19/2866399.html
- 【转】vlc android 代码编译
转自:http://blog.csdn.net/asircao/article/details/7734201 系统:ubuntu12.04代码:git://git.videolan.org/vlc- ...
- jquery 自动跳出列表
先上效果图:当鼠标经过相亲会自动弹出取最新的10条数据
- 一个简陋的 CSS 样式
有些网友对 Smart Framewok 中的 Sample 示例的样式比较感兴趣.由于本人对前端不太精通,但为了满足网友们的需求,只好献丑了. 以下这个简陋的 CSS 样式: ? 1 2 3 4 5 ...
- 《Swift开发指南》国内第一本Swift图书上市了
<Swift开发指南>国内第一本Swift图书上市了 既<courseId=799262">苹果Swift编程语言开发指南>视频教程地址:courseId=79 ...
- Java实现将指定目录内的指定类型的文件归类
这两天在学Java IO流,正好让我产生了将自己的电子书归类的打算,说做就做,Why not?看着自己所学所用能解决生活中的实际问题,是不是非常有成就感,那是必须的! package DepthSea ...
- css1-颜色和长度
<!DOCTYPE html>CSS1-颜色和长度 <style>div{ /*颜色*/ color:#f00; /*前景色*/ background:#00f; /*背景色* ...
- Intellij Idea 创建Web项目入门(一)转
Intellij Idea 创建Web项目入门(一) 相关软件: Intellij Idea14:http://pan.baidu.com/s/1nu16VyD JDK7:http://pan.bai ...
- x^2+y^2=N的整数解?
本文系转载:http://blog.sina.com.cn/s/blog_a661ecd50101cv41.html 我们先研究这个问题的一部分:哪些素数是两平方数之和?为什么我们先研究素数,有个很重 ...