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. Sqlite 设置默认时间为本地时间

    Sqlite 设置默认时间为本地时间 先设置字段类型为datetime, 再把缺省值设置为datetime( 'now', 'localtime' ) 代码查看如下 Time DATETIME DEF ...

  2. 九度OJ 1178:复数集合 (插入排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8393 解决:1551 题目描述: 一个复数(x+iy)集合,两种操作作用在该集合上: 1.Pop 表示读出集合中复数模值最大的那个复数,如 ...

  3. 去ioe

    http://baike.baidu.com/link?url=ntILcQyM_S7rpsbUrVu7vLEKHXNfSlJyWdWQnUo9LYO7JfoOpDEvbKldXobL0_nUEkXn ...

  4. ubuntu下安装redis以及redis客户端在mac下的使用

    ubuntu下安装redis http://blog.fens.me/linux-redis-install/ 此方式利用brew安装包去获取最新的rdm客户端 资源失效了 https://www.j ...

  5. Eclipse的.properties文件输出中文成unicode编码

    今天添加log4j.properties时,无法输入中文,输入的中文直接变成了unicode的编码形式.原因是Eclipse的.properties文件的默认编码为iso-8859-1. 选择Wind ...

  6. 在高通平台Android环境下编译内核模块【转】

    本文转载自:http://blog.xeonxu.info/blog/2012/12/04/zai-gao-tong-ping-tai-androidhuan-jing-xia-bian-yi-nei ...

  7. 深入浅出 - Android系统移植与平台开发(七)- 初识HAL【转】

    本文转载自:http://blog.csdn.net/mr_raptor/article/details/8069588 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   ...

  8. Spring Boot2.0之整合log4j

    传统方式打印日志比较复杂, 每次打印需要定义全局变量 private static final Logger logger = LoggerFactory.getLogger(SjpControlle ...

  9. 1 准备学习redis

    首先,当然是搜索相关资料了 1 Redis 设计与实现 http://redisbook.com/ 2 Redis快速入门 http://www.yiibai.com/redis/redis_quic ...

  10. Android SDK离线安装方法详解(加速安装)

    AndroidSDK在国内下载一直很慢··有时候通宵都下不了一点点,最后只有选择离线安装,现在发出离线安装地址和方法,希望对大家有帮助 一,首先下载SDK的安装包,android-sdk_r10-wi ...