UVa 12093 Protecting Zonk (树形DP)
题意:给定一个有n个节点的无根树,有两种装置A和B,每种都有无限多个。在某个节点X使用A装置需要C1的花费,并且此时与节点X相连的边都被覆盖。在某个节点X使用B装置需要C2的花费,并且此时与节点X相连的边以及与X相连的点相连的边都被覆盖。求覆盖所有边的最小花费。
析:树形DP,这是一个比较难想的,
dp[i][0] 表示 在 i 为根的子树不安装,且其子树的边都覆盖,
dp[i][1] 表示 在 i 处安装装置A
dp[i][2] 表示 在 i 处安装装置B
dp[i][3] 表示 在 i 为根的子树不安装,对于子结点不作要求,但对孙结点是要覆盖的
状态转移方程,第二个不好转移,其他的都好说
dp[i][0] += min(dp[v][1], dp[v][2]);
dp[i][2] += min(min(dp[v][0], min(dp[v][1], dp[v][2])), dp[v][3]) + C2;
dp[i][3] += min(dp[v][0], min(dp[v][1], dp[v][2]));
对于dp[i][1] 有两种转移方式,第一种,在 i 处真正的安装装置A,那么就是
dp[i][0] += min(dp[v][1], dp[v][2]);
还有一种,那就是它的子结点至少有一个安装装置B,那么就相当于 i 结点安装了装置A。
dp[i][2] += min(min(dp[v][0], min(dp[v][1], dp[v][2])), dp[v][3]) + det。
其中det表示最小的一个值在其子结点中安装装置B的花费。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10000 + 10;
const int mod = 1000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r > 0 && r <= n && c > 0 && c <= m;
}
int c1, c2; struct Edge{
int to, next;
};
Edge edge[maxn<<1];
int cnt, head[maxn];
int dp[maxn][4]; void addEdge(int u, int v){
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
} void dfs(int u, int fa){
dp[u][0] = dp[u][3] = 0;;
dp[u][1] = c1;
dp[u][2] = c2;
int mmin = INF, sum = 0;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(v == fa) continue;
dfs(v, u);
dp[u][0] += min(dp[v][1], dp[v][2]);
int t = min(dp[v][0], min(dp[v][1], dp[v][2]));
dp[u][1] += t;
dp[u][2] += min(t, dp[v][3]);
dp[u][3] += t;
sum += t;
mmin = min(mmin, dp[v][2] - t);
}
sum += mmin;
dp[u][1] = min(dp[u][1], sum);
} int main(){
while(scanf("%d %d %d", &n, &c1, &c2) == 3 && n){
cnt = 0; ms(head, -1);
for(int i = 1; i < n; ++i){
int u, v;
scanf("%d %d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
dfs(1, -1);
printf("%d\n", min(dp[1][0], min(dp[1][1], dp[1][2])));
}
return 0;
}
UVa 12093 Protecting Zonk (树形DP)的更多相关文章
- UVa 10859 - Placing Lampposts 树形DP 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVA - 1218 Perfect Service(树形dp)
题目链接:id=36043">UVA - 1218 Perfect Service 题意 有n台电脑.互相以无根树的方式连接,现要将当中一部分电脑作为server,且要求每台电脑必须连 ...
- UVa 1292 - Strategic game (树形dp)
本文出自 http://blog.csdn.net/shuangde800 题目链接: 点击打开链接 题目大意 给定一棵树,选择尽量少的节点,使得每个没有选中的结点至少和一个已选结点相邻. 思路 ...
- Uva LA 3902 - Network 树形DP 难度: 0
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- UVA 10859 Placing Lamppost 树形DP+二目标最优解的求解方案
题意:给定一个无向,无环,无多重边,要求找出最少的若干点,使得,每条边之中至少有一个点上有街灯.在满足上述条件的时候将还需要满足让两个点被选择的边的数量尽量多. 题解: 对于如何求解最小的节点数目这点 ...
- UVA - 1218 Perfect Service (树形dp)(inf相加溢出)
题目链接 题意:给你一个树形图,让你把其中若干个结点染成黑色,其余的染成白色,使得任意一个白色结点都恰好与一个黑色结点相邻. 解法比较容易,和树上的最大独立集类似,取一个结点作为树根,对每个结点分三种 ...
- UVA 10859 - Placing Lampposts 树形DP、取双优值
Placing Lampposts As a part of the mission ‘Beautification of Dhaka City’, ...
- 【Uva 12093】Protecting Zonk
[Link]: [Description] n个节点的树; 每个节点都可以选择3种 1.覆盖和它相连的边; c1花费; 2.覆盖和它相连的边以及和它相连的点相连的边; c2花费; 3.不进行操作 覆盖 ...
- 树形DP UVA 1292 Strategic game
题目传送门 /* 题解:选择一个点,它相邻的点都当做被选择,问最少选择多少点将所有点都被选择 树形DP:dp[i][0/1]表示当前点选或不选,如果选,相邻的点可选可不选,取最小值 */ /***** ...
随机推荐
- 手动更新花生壳域名的动态IP
http://帐号:密码@ddns.oray.com/ph/update?hostname=hahaha.vicp.cc&myip=182.xx.200.xx 花生壳的文档地址 例子 1.使用 ...
- 设置JAVA HOME环境变量的秕处理
echo "放在JDK目录下,win xp需要下载SETX.EXE" setx /m Java_Home "%cd%" setx /m ClassPath &q ...
- 黄聪:VPS服务器如何配置PHP.ini解决wordpress使用WP-Mail-SMTP插件发邮件出现Could not connect to SMTP host的解决办法
1.首先是WP-Mail-SMTP的下载地址:http://wordpress.org/plugins/wp-mail-smtp/ 2.出现Could not connect to SMTP host ...
- js cookie 工具类
/*cookie start*/ var Cookie=new function(){ //添加cookie this.add=function(name,value,hours){ var life ...
- 除去a标签target="_blank"的方法
用Jquery:$(function(){$("div>a").attr("target","_blank");});先查找页面上的d ...
- Oracle数据库物理结构
Oracle数据库物理结构 oracle的数据,实际上是以文件的形式来保存的,文件中出了保存用户的数据之外,还需要保存管理数据和日志数据等等.作为一个DBA,必须需要知道自己的数据分别保存在什么位置上 ...
- application/json 和 application/x-www-form-urlencoded的区别
public static string HttpPost(string url, string body) { //ServicePointManager.ServerCertificateVali ...
- 使用pandas时遇到ValueError: numpy.dtype has the wrong size, try recompiling
[问题]使用pandas时遇到ValueError: numpy.dtype has the wrong size, try recompiling [原因] 这是因为 Python 包的版本问题,例 ...
- vs2010 出现“未能将 ProteusDebugEngine 调试器附加到计算机”
vs2010 打开项目时出现如下图错误,解决方法: 1.查看C:\Progream Files下的Internet Explorer文件夹还在不在,不在则会出现此问题: 2.可以右键项目属性-调试-勾 ...
- apache 搭建PHP多站点
修改apache 配置文件:httpd.conf 1.默认 Listen 80端口 2.添加配置如下: <VirtualHost *:80> ServerAdmin admin@yii.c ...