POJ 3659 Cell Phone Network (树dp)
来源:互联网 发布:cdma网络是什么意思 编辑:程序博客网 时间:2024/05/21 11:02
大体题意:
给你一个棵树,要求选择尽量少的点,覆盖所有的点,选择一个点后,它相邻的点全都覆盖。
思路:
很明显树形dp。
令dp[i][0],表示 选择i 这个点, 覆盖了以i 为根的子树所有的点。
dp[i][1]表示 i 这个点不选,但选了 它的孩子一个结点,覆盖了以i 为根的子树所有的点。
dp[i][2]表示i 这个点不选,但选了它的父亲,覆盖了 以i 为根的子树的所有结点。
转移方程:
dp[i][0]很好说,既然选了i 这个点,那么它的孩子 选或不选 就随便了 dp[cur][0] += Min(dp[v][0],Min(dp[v][1],dp[v][2]));
dp[i][2] 也很好弄,i 不选,但选了它的父亲,那么i 已经被覆盖了, 不用管, 它的孩子选或不选都行了,dp[cur][2] += Min(dp[v][0],dp[v][1]);
dp[i][1] i 不选,选了它的孩子, 和dp[i][2]一样, 但是有一种情况 ,就是 全都选了dp[j][1],这样i 这个点就不被覆盖了。
这种情况单独考虑,必须找一个孩子选了。
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#define Min(a,b) ((a)<(b)?(a):(b))#define Max(a,b) ((a)>(b)?(a):(b))#define Siz(x) (int)x.size()using namespace std;const int maxn = 10000 + 10;const int inf = maxn*10;vector<int>g[maxn];int n;int dp[maxn][3];void dfs(int cur,int f){ if (Siz(g[cur]) == 1 && g[cur][0] == f){ dp[cur][0] = 1; dp[cur][1] = inf; dp[cur][2] = 0; return ; } dp[cur][0] = 1; dp[cur][2] = 0; dp[cur][1] = 0; bool ok = 0; int MIN = inf; int sum = 0; for (int i = 0; i < Siz(g[cur]); ++i){ int v = g[cur][i]; if (v != f){ dfs(v,cur); dp[cur][0] += Min(dp[v][0],Min(dp[v][1],dp[v][2])); dp[cur][2] += Min(dp[v][0],dp[v][1]); MIN = Min(MIN, dp[v][0] - dp[v][1]); sum += dp[v][1]; if (dp[v][0] > dp[v][1]){ dp[cur][1] += dp[v][1]; } else{ ok = 1; dp[cur][1] += dp[v][0]; } } } if (!ok){ dp[cur][1] = sum + MIN; }}int main(){ while(~scanf("%d",&n)){ for (int i = 1; i <= n; ++i)g[i].clear(); for (int i =1; i < n; ++i){ int u,v; scanf("%d %d",&u, &v); g[u].push_back(v); g[v].push_back(u); } dfs(1,-1); printf("%d\n",Min(dp[1][1],dp[1][0])); } return 0;}
Description
Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (conveniently numbered 1..N) so they can all communicate.
Exactly N-1 pairs of pastures are adjacent, and for any two pastures A and B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B) there is a sequence of adjacent pastures such that A is the first pasture in the sequence and Bis the last. Farmer John can only place cell phone towers in the pastures, and each tower has enough range to provide service to the pasture it is on and all pastures adjacent to the pasture with the cell tower.
Help him determine the minimum number of towers he must install to provide cell phone service to each pasture.
Input
* Line 1: A single integer: N
* Lines 2..N: Each line specifies a pair of adjacent pastures with two space-separated integers: A and B
Output
* Line 1: A single integer indicating the minimum number of towers to install
Sample Input
51 35 24 33 5
Sample Output
2
Source
[Submit] [Go Back] [Status] [Discuss]
- POJ 3659 Cell Phone Network (树dp)
- [树形DP] poj 3659 Cell Phone Network
- POJ 3659 Cell Phone Network(树形DP)
- poj 3659 Cell Phone Network(树dp+树的最小支配集)
- poj 3659 Cell Phone Network(树的最小支配集树形DP)
- POJ 3659 Cell Phone Network(贪心)
- poj 3659 Cell Phone Network (树最小支配集)
- POJ 3659 Cell Phone Network
- POJ-3659-Cell Phone Network
- POJ 3659 Cell Phone Network
- POJ 3659 Cell Phone Network
- POJ 3659 Cell Phone Network【最小支配集 dp && 贪心】
- POJ 3659 Cell Phone Network【最小支配集、树型dp】
- poj 3659 Cell Phone Network USACO 树形DP
- poj 3659 Cell Phone Network 树型DP也是树的最小支配集
- POJ 3659 Cell Phone Network 最小支配集模板题(树形dp)
- poj3659 Cell Phone Network(树形dp)
- POJ 3659——Cell Phone Network
- Mysql索引的使用
- C++之引用(2)
- ST表LCA模板
- mysqli操作数据库常用语句
- Linux下的软连接和硬链接
- POJ 3659 Cell Phone Network (树dp)
- ambari安装hive遭遇Failed to get schema version
- DPDK中文-DPDK工具(procinfo)
- 《剑指Offer》面试题一之赋值运算符函数
- Java-Basic-易混淆的概念与方法
- 封装$选择器
- 各种大型网站技术架构
- Android基于HLS和RTMP协议的第三方SDK选择
- 子元素margin-top为何会影响父元素?