zoukankan      html  css  js  c++  java
  • 【POJ 3659】Cell Phone Network

    Cell Phone Network
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6012   Accepted: 2163

    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 ≤ NA ≠ B) there is a sequence of adjacent pastures such that 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
    

    Source

     
    真是蛋疼啊。理解错题意了。以为是覆盖边了。后来看了神犇的BLOG才恍然大悟。。。
    还是DP;
    以任意节点为根开始DFS+DP(就是树形DP嘛)
    3种情况,,
      (1)编号为x的节点建塔,保证x和其子孙被基塔覆盖。
      (2)编号为x的节点不建塔,保证x和其子孙被基塔覆盖。
      (3)编号为x的节点不建塔,保证x没有被覆盖,但x的子孙已被覆盖。
    就这么多,不难写出转移方程了。
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    const int MAXN = 10005;
    
    struct Edge
    {
        int from, to, next;
        
        Edge() {
        }
        
        Edge(int u, int v) : from(u), to(v) {
        }
        
    }edges[MAXN << 1];
    
    int first[MAXN], tots;
    int f[MAXN][3], n;
    
    void add(int a, int b)
    {
        edges[tots] = Edge(a, b);
        edges[tots].next = first[a];
        first[a] = tots++;
    }
    
    void dfs(int x)
    {
        memset(f[x], 0, sizeof(f[x]));
        bool flag = true;
        int mint = 0x7fffffff;
        for (int i = first[x]; i != -1; i = edges[i].next)
        {
            Edge &e = edges[i];
            if (f[e.to][0] == -1)
            {
                dfs(e.to);
                f[x][0] += min(f[e.to][0], min(f[e.to][1], f[e.to][2]));
                f[x][2] += min(f[e.to][0], f[e.to][1]);
                if (f[e.to][0] <= f[e.to][1])
                {
                    flag = false;
                    f[x][1] += f[e.to][0];
                }
                else
                {
                    mint = min(mint, f[e.to][0] - f[e.to][1]);
                    f[x][1] += f[e.to][1];
                }
            }
        }
        if (flag) f[x][1] += mint;
        f[x][0]++;
    }
    
    int main()
    {
        scanf("%d", &n);
        tots = 0;
        memset(first, -1, sizeof(first));
        for (int i = 1; i < n; ++i)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            add(u, v);
            add(v, u);
        }
        memset(f, -1, sizeof(f));
        dfs(1);
        printf("%d
    ", min(f[1][0], f[1][1]));
        return 0;
    }
  • 相关阅读:
    ASP.NET Forms 身份验证概述
    JS中变量相关的细节分析
    document对象execCommand的命令参数介绍
    一点一点学ASP.NET之基础概念——HTTP运行期与页面执行模型
    读《大道至简》第一章有感
    读大道至简第二章有感
    课程作业2
    编写一个程序,用户输入两个数,求其加减乘除,并用消息框显示计算结果。
    201920201 20209324《Linux内核原理与分析》第一周作业
    jQuery Plugins
  • 原文地址:https://www.cnblogs.com/albert7xie/p/4804173.html
Copyright © 2011-2022 走看看