zoukankan      html  css  js  c++  java
  • 洛谷P2899 [USACO08JAN]手机网络Cell Phone Network

    P2899 [USACO08JAN]手机网络Cell Phone Network

    题目描述

    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 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.

    John想让他的所有牛用上手机以便相互交流(也是醉了。。。),他需要建立几座信号塔在N块草地中。已知与信号塔相邻的草地能收到信号。给你N-1个草地(A,B)的相邻关系,问:最少需要建多少个信号塔能实现所有草地都有信号。

    输入输出格式

    输入格式:
    • 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
    输出格式:
    • Line 1: A single integer indicating the minimum number of towers to install

    输入输出样例

    输入样例#1:
    5
    1 3
    5 2
    4 3
    3 5
    
    输出样例#1:
    2
    /*
        f[i][0]表示节点i不选但被覆盖 
        f[i][1]表示节点i选
        f[i][2]表示节点i不选也不被覆盖
    */
    #include<iostream>
    #include<cstdio>
    #define maxn 10010
    #define INF 2000000000
    using namespace std;
    int f[maxn][3],n,num,head[maxn];
    struct node{
        int to,pre;
    }e[maxn*2];
    void Insert(int from,int to){
        e[++num].to=to;
        e[num].pre=head[from];
        head[from]=num;
    }
    void dfs(int now,int father){
        int f0=INF,f2=0,f1=0,w=0,s=0;
        for(int i=head[now];i;i=e[i].pre){
            int to=e[i].to;
            if(to==father)continue;
            dfs(to,now);
            s=min(f[to][0],f[to][1]);
            w+=s;
            f0=min(f0,f[to][1]-s);
            f1+=min(f[to][1],min(f[to][0],f[to][2]));
            if(f2<INF)f2+=f[to][0];
        }
        f[now][1]=f1+1;f[now][2]=f2;
        if(f0==INF)f[now][0]=INF;
        else f[now][0]=w+f0;
    }
    int main(){
        int x,y;
        scanf("%d",&n);
        for(int i=1;i<n;i++){
            scanf("%d%d",&x,&y);
            Insert(x,y);
            Insert(y,x);
        }
        dfs(1,0);
        int ans=min(f[1][1],f[1][0]);
        cout<<ans;
    }
  • 相关阅读:
    poi生成excel整理(设置边框/字体/颜色/加粗/居中/)
    java里poi操作Excel工具类【我改】
    java里poi操作excel的工具类(兼容各版本)
    在java poi导入Excel通用工具类示例详解
    layui给select下拉框赋值
    Unity3d开发wp8问题汇总
    Unity3D WP8发布解决方案名 DefaultPackageName 修改问题
    微软发布WP SDK8.0 新增语音、应用内支付等原生API
    unity3d android互调
    解决unity3d发布的网页游戏放到服务器上无法使用的问题
  • 原文地址:https://www.cnblogs.com/thmyl/p/7527267.html
Copyright © 2011-2022 走看看