Problem I: Plants vs. Zombies HD Super Pro

Plants versus Zombies HD Super Pro is a game played not a grid, but on a connected graph G with no cycles (i.e., a tree). Zombies live on edges of the tree and chew through edges so that tree falls apart! Plants can be purchased and placed on vertices of the tree to protect the tree from falling apart. It is not possible to plant more than one plant on the same vertex. A plant protects one or more adjacent edges, depending on the strength and capabilities of the plant.

The Almanac offers you to buy any of three different types of plants:

  • PEASHOOTERS: These are your first line of defense and can shoot peas along any one edge (of your choosing) adjacent to the vertex upon which it is placed. Cost: $100 per plant.
  • SPLIT PEAS: These are hard working pea shooters and can shoot peas along any two edges (of your choosing) adjacent to the vertex upon which it is placed. Cost: $175 per plant.
  • STARFRUIT: Having just visited the dentist, a STARFRUIT is very upset and shoots stars along all edges adjacent to the vertex upon which it is placed. Cost: $500 per plant.

Your goal is to protect the tree from the Zombies by having every edge covered by at least one plant, and doing so spending the least amount of money. You can buy more than one of each type of plant, but you can only plant at most one plant on each vertex.

Input Format

The input starts with an integer T - the number of test cases (T <= 100). T cases follow, each starting with the integer N on the first line, the number of vertices in G (2 <= N <= 10,000). N-1 line follows, each containing two space separated integers u and v (0 <= u,v <= N-1, u ≠ v) - describing an edge.

Output Format

For each test case, print on a separate line the minimum cost of protecting the tree, formatted like in the sample output.

Sample Input

2
2
0 1
3
0 1
1 2

Sample Output

$100
$175

In the second case we can put a Split Pea on the vertex 1.


题意: 给一颗n个点的树,每个点上只能放一种植物,共有三种植物,第一种可以覆盖与种植点相邻的一条边,费用是100,第二种是可以覆盖两条边,费用是175,第三种是可以覆盖所有边,费用500。求最小花费的金额,使得树上每一条边都能被覆盖。

思路:果断树DP。设dp[u][0]表示以u为根节点且u不覆盖u到他父亲节点的边的最小费用,dp[u][1]表示以u为根节点且u覆盖u到他父亲节点的边的最小费用。

然后分四种情况考虑状态转移。以下用v表示u的子节点

1.u不种植物:dp[u][0] = dp[v][1],而且不种植物是不可能连接父节点,即无dp[u][1]

2.u种第一种: dp[u][0] = dp[v][0] (选一个子节点) + dp[v][1] (剩余的所有子节点之和) + cost[1]

dp[u][1] = dp[v][1] (所有子节点之和) + cost[1]

3.u种第二种: dp[u][0] = dp[v][0](选两个子节点) + dp[v][1] (剩余所有子节点) + cost[2]

dp[u][1] = dp[v][0] (选一个子节点) + dp[v][1] (剩余所有子节点) + cost[2]

4.u种第三种: dp[u][0] 是不可能的

dp[u][1] = dp[v][0/1](所有子节点) + cost[3]

至于选择哪一个子节点,这里要用贪心,选一个差值最大的来搞,记录下sum1和sum0,以及最大差值dmx,次大差值dmx2

注意下使用第三种植物的时候不能简单用sum1和sum0,因为有dp[v][1] < dp[u][0]的情况存在,今天比赛就是坑这个位置了,遗憾啊。。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 1e9;
const int N = ; struct _edge{
int v,next;
};
_edge edge[N*];
int first[N],n,ecnt;
int dp[N][];
int cost[]; inline void add(int u,int v)
{
edge[ecnt].v = v;
edge[ecnt].next = first[u];
first[u] = ecnt++;
} void dfs(int u,int fa)
{
dp[u][] = dp[u][] = INF;
bool flag = ;
int sum1,sum0,dmx,dmx2;
sum1=sum0=;
int sum3=;
dmx=dmx2=;
for(int e=first[u];e!=-;e=edge[e].next)
{
int v = edge[e].v;
if(v==fa) continue;
flag = ;
// 1:
dfs(v,u);
sum1 += dp[v][];
sum0 += dp[v][];
sum3 += min(dp[v][],dp[v][]);
int d = dp[v][]-dp[v][];
if(d>dmx2)
{
dmx2 = d;
if(dmx2 > dmx)
{
swap(dmx,dmx2);
}
}
}
if(flag)
{
dp[u][]=;
dp[u][]=cost[];
return;
}
dp[u][] = min(sum1 - dmx + cost[],sum1 - dmx - dmx2 + cost[]);
dp[u][] = min(dp[u][],sum1);
dp[u][] = min(min(sum1 + cost[],sum1 - dmx + cost[]),sum3 + cost[]);
} void run()
{
memset(first,-,sizeof(first));
ecnt=;
scanf("%d",&n);
int u,v;
for(int i=;i<n;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(,-);
// for(int i=0;i<n;i++)
// {
//// printf("%d: %d %d\n",i,dp[i][0],dp[i][1]);
// }
printf("$%d\n",min(dp[][],dp[][]));
} int main()
{
//freopen("in","r",stdin);
cost[]=;cost[]=;cost[]=;
int _;
scanf("%d",&_);
while(_--)
run();
return ;
}

uva 12452 Plants vs. Zombies HD SP (树DP)的更多相关文章

  1. ZOJ 4062 Plants vs. Zombies(二分答案)

    题目链接:Plants vs. Zombies 题意:从1到n每个位置一棵植物,植物每浇水一次,增加ai高度.人的初始位置为0,人每次能往左或往右走一步,走到哪个位置就浇水一次.求m步走完后最低高度的 ...

  2. Plants vs. Zombies(二分好题+思维)

    Plants vs. Zombies http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5819 BaoBao and DreamG ...

  3. zoj4062 Plants vs. Zombies 二分+模拟(贪心的思维)

    题目传送门 题目大意:有n个植物排成一排,标号为1-n,每株植物有自己的生长速度ai,每对植物浇一次水,该株植物就长高ai,现在机器人从第0个格子出发,每次走一步,不能停留,每一步浇一次水,总共可以走 ...

  4. bzoj 3572世界树 虚树+dp

    题目大意: 给一棵树,每次给出一些关键点,对于树上每个点,被离它最近的关键点(距离相同被标号最小的)控制 求每个关键点控制多少个点 分析: 虚树+dp dp过程如下: 第一次dp,递归求出每个点子树中 ...

  5. bzoj 2286 [Sdoi2011]消耗战 虚树+dp

    题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...

  6. 51nod1812树的双直径(换根树DP)

    传送门:http://www.51nod.com/Challenge/Problem.html#!#problemId=1812 题解:头一次写换根树DP. 求两条不相交的直径乘积最大,所以可以这样考 ...

  7. bzoj1791[IOI2008]Island岛屿(基环树+DP)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1791 题目大意:给你一棵n条边的基环树森林,要你求出所有基环树/树的直径之和.n< ...

  8. [bzoj2878][Noi2012]迷失游乐园(基环树dp)

    [bzoj2878][Noi2012]迷失游乐园(基环树dp) bzoj luogu 题意:一颗数或是基环树,随机从某个点开始一直走,不走已经到过的点,求无路可走时的路径长期望. 对于一棵树: 用两个 ...

  9. CF456D A Lot of Games (字典树+DP)

    D - A Lot of Games CF#260 Div2 D题 CF#260 Div1 B题 Codeforces Round #260 CF455B D. A Lot of Games time ...

随机推荐

  1. java 给多人发送、抄送

    关键技术: 1.MimeMessage的setRecipients方法设置邮件的收件人,其中Message.RecipientType.TO常量表示收件人类型是邮件接收者,Message.Recipi ...

  2. 动态内存分配(Dynamic memory allocation)

    下面的代码片段的输出是什么?为什么? 解析:这是一道动态内存分配(Dynamic memory allocation)题.    尽管不像非嵌入式计算那么常见,嵌入式系统还是有从堆(heap)中动态分 ...

  3. 说明sizeof和strlen之间的区别。

    解析:由以下几个例子我们说明sizeof和strlen之间的区别.第1个例子: sizeof(ss)结果为4,ss是指向字符串常量的字符指针.sizeof(*ss)结果为1,*ss是第一个字符.第2个 ...

  4. IOS开发之----异常处理

    本文转载至 http://blog.csdn.net/chenyong05314/article/details/7906593 转载自:http://blog.sina.com.cn/s/blog_ ...

  5. WCP源码分析 与SpringMVC学习资料

    1.在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便. Spring2.5为我们引入了组件自动扫描机制,他可以在 ...

  6. 使用T4模板技术

    1.右键->添加->新建项,选择“文本模板” 2.修改代码为: <#@ template debug="false" hostspecific="fal ...

  7. samba服务器的搭建和配置

    案例: 公司有两个部门, sales / market . 分别有成员 jack / tom 和 zhang / shen . 公司需求是这样的, 本部门资料禁止其他部门访问, 本部门成员之间不能干扰 ...

  8. 性能测试--Jmeter之wordpress示例

    Jmeter之wordpress示例 WordPress是使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站.也可以把 WordPress当作一个内容管理 ...

  9. JSP&Servlet(转)

    第一篇:Web应用基础1.概念:  1.1应用程序分类     a.桌面应用程序:一般是指采用client/server即客户机/服务器结构的应用程序.     b.web应用程序:一般是指采用Bro ...

  10. Flask中的模板语法jinjia2

    Flask中默认的模板语言是Jinja2 I. Jinja2模板语言中的 for {% for foo in g %} {% endfor %} II. Jinja2模板语言中的 if {% if g ...