HDU 4340
好题一道啦。做这题时,抓住两个问题:一、给某点染色时,其连通的点是否已有一点以全部代价染色。二、该点染什么颜色。
嗯。第二个问题很容易,但第一个问题不容易了。我一开始就考虑祖父子三层结点的关系,认为在父结点时,要么子结点已染色,要么父结点已染色%#……*&¥%#……
很复杂。。。。
这是条件定得过于严格了,所以走不通。看了题解后,发现,可以把状态条件设得宽一点。
设dp[rt][0][0]为以rt为根的子树内,rt染0颜色,与其连通的块(同色,在子树内的)中,没有一个节点是以全部代价染色的,即没有入口点。
dp[rt][0][1]则为以rt为根的子树内,rt染0颜色,与其连通的块(同色,在子树内的)中,存在一个节点是以全部代价染色的。感觉这个存在实现设得妙,只要存在就可以,条件放宽了许多。
对于dp[r][1][0],dp[r][1][1]有同样的定义。
于是dp[rt][0][0]=sum(min(dp[son][0][0],dp[son][1][1]))+costrt/2;
dp[rt][0][1]=sum(min(dp[son][0][0],dp[son][1][1]))+min(costrt,costrt/2+min(dp[son][0][1]-min(dp[son][0][0],dp[son][1][1]))).解释一下min(dp[son][0][1]-min(dp[son][0][0],dp[son][1][1])。这是把子结点以根的子树状态转化为dp[rt][0][1]的最小代价,其中min(dp[son][0][0],dp[son][1][1])与方程开始的min(dp[son][0][0],dp[son][1][1])是一致的,因而又是一个巧妙的地方。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#define N 110 using namespace std;
int costA[N],costB[N]; int dp[N][][]; struct edge{
int u,v;
int next;
}Edge[N*];
int n,tot;
int head[N]; void addedge(int u,int v){
Edge[tot].u=u;
Edge[tot].v=v;
Edge[tot].next=head[u];
head[u]=tot++;
} void dfs(int now,int pre){
bool leaf=true;
int sum0=,sum1=;
int CA=(<<),CB=(<<);
for(int e=head[now];e!=-;e=Edge[e].next){
int v=Edge[e].v;
if(v!=pre){
leaf=false;
dfs(v,now);
sum0+=min(dp[v][][],dp[v][][]);
sum1+=min(dp[v][][],dp[v][][]);
CA=min(CA,dp[v][][]-min(dp[v][][],dp[v][][])+costA[now]/);
CB=min(CB,dp[v][][]-min(dp[v][][],dp[v][][])+costB[now]/);
}
}
if(leaf){
dp[now][][]=costA[now]/;
dp[now][][]=costA[now];
dp[now][][]=costB[now]/;
dp[now][][]=costB[now];
}
else{
dp[now][][]=sum0+costA[now]/;
dp[now][][]=sum0+min(costA[now],CA);
dp[now][][]=sum1+costB[now]/;
dp[now][][]=sum1+min(costB[now],CB);
}
} int main(){
int u,v;
while(scanf("%d",&n)!=EOF){
tot=;
memset(head,-,sizeof(head));
for(int i=;i<=n;i++)
scanf("%d",&costA[i]);
for(int i=;i<=n;i++)
scanf("%d",&costB[i]);
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dfs(,);
printf("%d\n",min(dp[][][],dp[][][]));
}
return ;
}
HDU 4340的更多相关文章
- hdu 4340 树状DP
思路:我们定义两个数组,ant[Maxn][2],bob[Maxn][2].ant[i][0]表示还未确定哪个城市被全费用占领,ant[i][1]表示确定了哪个城市被全费用占领.那么ant[i][0] ...
- 【HDU - 4340】Capturing a country(树形DP)
BUPT2017 wintertraining(15) #8A 题意 n(<100)个城市组成的树.A攻击i城市需要a[i]代价,B需要b[i].如果一个城市的邻居被A攻击了,那么A攻击它只要A ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
随机推荐
- 2016.04.07,英语,《Vocabulary Builder》Unit 11
cant, from the Latin verbs canere and cantare, meaning 'sing'. by way of French, add an h to the roo ...
- HDU 4386 Quadrilateral(数学啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4386 Problem Description One day the little Jack is p ...
- [POI 2007] 堆积木
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1109 [算法] DP [代码] #include<bits/stdc++.h& ...
- Node.js:Web 模块
ylbtech-Node.js:Web 模块 1.返回顶部 1. Node.js Web 模块 什么是 Web 服务器? Web服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,Web服 ...
- 使用JDK和axis2发布webservice
最近使用webservice进行远程调用一直很火,自从JDK1.6版本发布后,发布一个webservice项目变得更加简单了 笔者由于工作的需要针对JDK和axis2如何发布webservice做过相 ...
- PostgreSQL的HA解决方案-2负载均衡(load balance)
一.部署说明 1.1 实施环境 本文档实验环境如下: PGSQL主机: 192.168.1.45 PGSQL备机: 192.168.1.50 软件和系统版本 Pgsql 版本: pgsql 9.2.4 ...
- spring之interceptor篇
springmvc中要写一个拦截器非常的简单,有两种方式:要么实现HandlerInterceptor接口或者继承实现了该接口的类,如spring已经为我们写好的一个HandlerIntercepto ...
- Kattis - Different Distances
Input The input file contains up to 10001000 test cases, each of which contains five real numbers, x ...
- JS 封装一个求n~m的求和函数
var a = 0; cc(2,10); function cc(n,m){ for(var i =n;i<(m+1);i++){ a = a + ...
- Java Spring 两大特色
0 引言 本文主要描述的是Spring常用的两大特色功能:AOP和IoC容器 1 IoC Spring的IoC:就是常说的“控制反转”,也又叫依赖注入的(DI). 优点:IoC最大的好处就是把对象生成 ...