ACM: 动态规划题 poj 3659

Cell Phone Network
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 ≤ AN; 1 ≤ BN; AB) there is a sequence of adjacent pastures such that A is the first pasture in the sequence and B is 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

5
1 3
5 2
4 3
3 5

Sample Output

2

题意: 一棵树种, 每个节点可以覆盖相邻的节点, 用最少的节点覆盖整棵树.

      计算最少的节点数量.

解题思路:

      1. 一开始把题意给理解错了, 误认为每个节点可以覆盖相邻的边, 即可覆盖相邻节点.

         只考虑每个节点两种状态-建塔和不建, 这样结果会使得结果不是最少的. WA也是肯定的.

      2. 应该考虑三种情况:

         dp[i][0]: 表示在以i为子树根, 节点i建塔的最少建塔数量.

         dp[i][1]: 表示在以i为子树根, 在子节点建塔来覆盖i的最少建塔数量.

         dp[i][2]: 表示在以i为子树根, 在父节点建塔来覆盖i的最少建塔数量.

        初始化--考虑叶子节点

         dp[leaf][0] = 1. 叶子自己建塔覆盖自己.

         dp[leaf][1] = INF(无穷大). 因为叶子节点不存在后代.

         dp[leaf][2] = 0. 因为父亲节点可以覆盖这棵子树(叶子节点一个而已).

        状态方程:

         (1). dp[i][0] += min(dp[v][0], dp[v][1], dp[v][2])

         当前节点i自己建塔覆盖自己, 因此不考虑自身覆盖问题. 只取子树最小值.

         (2). dp[i][1] = min(dp[v][0], dp[v][1])

         当前节点用一个子节点建塔来覆盖自己, 但是会出现一种情况, 全部的子节点都是依赖

         它本身的子节点覆盖自己(全部v节点满足: dp[v][0]>dp[][1]), 因此i节点需要选择一

         个子节点来建塔覆盖i, 当然这个子节点满足最小值的情况.

         (3). dp[i][2] = min(dp[v][0], dp[v][1]);

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 20005
const int INF = (1<<29);

struct node
{
 int v;
 int next;
}edges[MAX];

int n;
int first[MAX], num, d[MAX];
int dp[MAX][3];
bool vis[MAX];

inline int min(int a, int b)
{
 return a < b ? a : b;
}

inline void add(int u, int v)
{
 edges[num].v = v;
 edges[num].next = first[u];
 first[u] = num++;
}

void read_graph()
{
 memset(dp, 0, sizeof(dp));
 memset(d, 0, sizeof(d));
 memset(first, -1, sizeof(first));
 memset(vis, false, sizeof(vis));
 num = 0;
 int u, v;
 for(int i = 1; i < n; ++i)
 {
  scanf("%d %d",&u, &v);
  add(u, v);
  add(v, u);
  d[u]++;
  d[v]++;
 }
}

void dfs(int u)
{
 vis[u] = true;
 if(u != 1 && d[u] == 1)
 {
  dp[u][0] = 1; //u上建
  dp[u][1] = INF; //u的子节点建
  dp[u][2] = 0; // u的父亲节点建
  return ;
 }

 int temp1 = INF, temp2;
 int flag = false;
 dp[u][0] = 1;
 for(int e = first[u]; e != -1; e = edges[e].next)
 {
  int v = edges[e].v;
  if( !vis[v] )
  {
   dfs(v);
   dp[u][0] += min(dp[v][0], min(dp[v][1], dp[v][2]));
   if( dp[v][0] > dp[v][1] ) //子节点选择它的子节点建
   {
    dp[u][1] += dp[v][1];
    if(temp1 > dp[v][0])
    {
     temp1 = dp[v][0];
     temp2 = dp[v][1];
    }
   }
   else
   {
    dp[u][1] += dp[v][0];
    flag = true;
   }
   dp[u][2] += min(dp[v][0], dp[v][1]);
  }
 }
 if( !flag ) dp[u][1] = (dp[u][1]-temp2)+temp1;
}

int main()
{
// freopen("input.txt", "r", stdin);
 while(scanf("%d", &n) != EOF)
 {
  read_graph();
  if(n == 1)
  {
   printf("1\n");
   continue;
  }
  dfs(1);

  printf("%d\n", min(dp[1][0], dp[1][1]));
 }
 return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值