Protecting Zonk UVA
来源:互联网 发布:查看论文的软件 编辑:程序博客网 时间:2024/06/04 18:37
题意:
给定一个有n个节点的无根树,有两种装置A和B,每种都有无限多个。在某个节点X使用A装置需要C1的花费,并且此时与节点X相连的边都被覆盖。在某个节点X使用B装置需要C2的花费,并且此时与节点X相连的边以及与X相连的点相连的边都被覆盖。求覆盖所有边的最小花费。
树形DP的题目写多了后这种模板题还是很好想的。。
然而我写(智)的(商)不(捉)多(鸡) 想了一会 想出了个及其复杂的状态转移方程。。 然后调了半天发现我那么定义状态实在不好转移。。 于是选择放弃,看了题解。。。。
///dp[u][0]:u没有安装装置,且u的子节点下的边都被覆盖,且未覆盖其父节点和他连的那条边
///dp[u][1]:u刚好可以覆盖其父节点和他连的那条边
///dp[u][2]:u安装装置B
///dp[u][3]:u没有安装装置,其可能子边未完全覆盖
ORZ 为什么大佬定义的状态都这么的简洁有效啊! /(ㄒoㄒ)/~~
# include<iostream># include<cstdio># include<cstring># include<algorithm>using namespace std;const int MAX=10005;int dp[MAX][6];class node{public: int u,v,next;};node edge[MAX<<1];int head[MAX];int tot=0;void add(int u,int v){ edge[tot].next=head[u]; edge[tot].u=u; edge[tot].v=v; head[u]=tot++;}int c1,c2;void dfs(int u,int per){ dp[u][0]=0,dp[u][1]=c1,dp[u][2]=c2,dp[u][3]=0; int mins=1<<30;int sum=0; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; int cnt=0; if(v==per) continue; dfs(v,u); ///dp[u][0]:u没有安装装置,且u的子节点下的边都被覆盖,且未覆盖其父节点和他连的那条边 ///dp[u][1]:u刚好可以覆盖其父节点和他连的那条边 ///dp[u][2]:u安装装置B ///dp[u][3]:u没有安装装置,其可能子边未完全覆盖 dp[u][2]+=min(dp[v][0],min(dp[v][1],min(dp[v][2],dp[v][3]))); dp[u][0]+=(dp[v][1]); dp[u][1]+=(cnt=min(dp[v][0],min(dp[v][2],dp[v][1]))); dp[u][3]+=min(dp[v][0],min(dp[v][1],dp[v][2])); mins=min(mins,dp[v][2]-cnt); sum+=cnt; } sum+=mins; dp[u][1]=min(dp[u][1],sum);}int main(){ int n; while(~scanf("%d%d%d",&n,&c1,&c2)) { if(n==0 && c1==0 &&c2==0) break; memset(dp,0,sizeof dp); memset(head,-1,sizeof head); tot=0; for(int i=1;i<n;i++) { int u,v; scanf("%d %d",&u,&v); add(u,v); add(v,u); } dfs(1,-1); cout<<min(dp[1][1],min(dp[1][2],dp[1][0]))<<endl; }}
阅读全文
0 0
- Protecting Zonk UVA
- Protecting Zonk UVA
- Uva 12093 Protecting Zonk
- uva 12093 Protecting Zonk 树形dp
- Uva-12093 Protecting Zonk(较复杂的树形DP)
- UVa12093 Protecting Zonk
- uva 12093 Protecting Zonk(在某个节点X使用A装置,此时与节点X相连的边都被覆盖)
- 7.6. Protecting a Host
- Castle Protecting(dp)
- Protecting HDFS Metadata
- Protecting the Flowers(3262)
- POJ3262-Protecting the Flowers
- [Usaco07Jan] Protecting the Flowers
- [POJ3262]Protecting the Flowers
- POJ Protecting the Flowers
- Protecting the Flowers
- Protecting the Flowers (贪心)
- Protecting the Flowers
- 【专题】二分图最大匹配(匈牙利算法)
- 【原创】无线破解Aircrack-ng套件详解--airmon-ng与airodump-ng
- sqlmap的简要使用方法
- hydra简单使用示例
- shell编程学习笔记之标准输入输出(read&echo)
- Protecting Zonk UVA
- shell编程学习笔记之特殊变量($0、$1、$2、 $?、 $# 、$@、 $*)
- shell编程学习笔记--整数自增
- shell编程学习笔记之正则表达式初识
- 第二天:html中块行级、margin和padding、盒模型、css选择器和样式
- 编写一个简单的进程池
- shell编程学习笔记之sed编辑器
- shell小脚本--网速监控
- 【转】获取Windows系统明文密码神器