zoukankan      html  css  js  c++  java
  • POJ3659 Cell Phone Network

    POJ3659 Cell Phone Network
    Time Limit: 1000MS Memory Limit: 65536K
    Total Submissions: 3782 Accepted: 1305

    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

    *********************************************************************
    题目大意:给定一棵树,求最小支配集。就是说用最少的点覆盖这棵树所有的边。
    解题思路:发现,我的想法有点奇葩,和大家的一般思路不太一样。我对每个点设立了三种状态:0表示这个点没有覆盖正期待着被父亲节点覆盖,1表示在当前节点就是属于支配集,2表示当前节点被他的儿子覆盖。
    状态转移:    dp[root][0]=sum{dp[son][2]};
          dp[root][1]=sum{min{dp[son][0],dp[son][1],dp[son][2]}};
          dp[root][2]=sum{min{dp[son][1],dp[son][2]}};这一条比较特殊,当括号内取小的时候如果一直取的是dp[son][2],就需要特殊处理,就是说必须有一个点是dp[son][1]的取值。还有就是这个方法的边界处理比较麻烦。
    #include <stdio.h>
    #include <vector>
    #include <string.h>
    #define N 10005
    #define INF 2000000
    #define MIN(a,b) ((a)<(b)?(a):(b))
    using namespace std;
    
    vector<int>gra[N];
    int fa[N],dp[N][3],n;
    
    void dfs(int s,int f)
    {
        fa[s]=f;
        dp[s][0]=0;
        dp[s][1]=1;
        dp[s][2]=0;
        if(gra[s].size()==1&&gra[s][0]==f)
        {
            dp[s][2]=INF;
            return ;
        }
        int mark=0;
        for(int i=0;i<gra[s].size();i++)
        {
            int t=gra[s][i];
            if(t==f)continue;
            dfs(t,s);
            dp[s][1]+=MIN(MIN(dp[t][0],dp[t][1]),dp[t][2]);
            dp[s][0]+=dp[t][2];
            if(dp[t][1]<=dp[t][2])
            {
                mark=1;
                dp[s][2]+=dp[t][1];
            }
            else
                dp[s][2]+=dp[t][2];
        }
        if(mark)return ;
        int temp=INF;
        for(int i=0;i<gra[s].size();i++)
        {
            int t=gra[s][i];
            if(t==f)continue;
            temp=MIN(temp,dp[s][2]+dp[t][1]-dp[t][2]);
        }
        if(temp!=INF)
            dp[s][2]=temp;
    }
    
    void re(void)
    {
        scanf("%d",&n);
        memset(fa,0,sizeof(fa));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
            gra[i].clear();
        for(int i=1;i<n;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            gra[a].push_back(b);
            gra[b].push_back(a);
        }
    }
    
    void run(void)
    {
        dfs(1,-1);
        printf("%d\n",MIN(dp[1][1],dp[1][2]));
    }
    
    int main()
    {
        re();
        run();
    }
    

      

  • 相关阅读:
    xmselect动态渲染(单选和多选)
    Python数据挖掘学习进度11(pandas高级处理)
    Mysql:DML-增删改表中数据
    Mysql:DDL-操作数据库、表
    mybatis实现增删改查操作
    mybatis的入门(环境的部署)
    Redis
    web综合案例:登录界面,注册界面,忘记密码等功能实现
    git常用命令实践
    在IntelliJ IDEA中使用git
  • 原文地址:https://www.cnblogs.com/Fatedayt/p/2192169.html
Copyright © 2011-2022 走看看