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;
    }
  • 相关阅读:
    webpack4.0在项目中的安装配置
    Java调用开源GDAL解析dxf成shp,再调用开源GeoTools解析shp文件
    VUE-CLI3.0组件封装打包使用
    鼠标光标在input框,单击回车键后防止页面刷新的问题
    MapBox GL加载天地图以及加载导航控件
    web前端监控视频的展示
    css外部字体库文件的引用
    IIS上部署的程序,PLSQL能连上数据库,系统登录报错
    部署在IIS上的程序,可以找到文件夹,能看到文件却报404
    继承
  • 原文地址:https://www.cnblogs.com/albert7xie/p/4804173.html
Copyright © 2011-2022 走看看