【贪心】【树形DP】[POJ1463][HDU1054]Strategic game 战略游戏

10 篇文章 0 订阅
7 篇文章 0 订阅

Description

Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?

Your program should find the minimum number of soldiers that Bob has to put for a given tree.

For example for the tree:

the solution is one soldier ( at the node 1).

Input

The input contains several data sets in text format. Each data set represents a tree with the following description:

the number of nodes
the description of each node in the following format
node_identifier:(number_of_roads) node_identifier1 node_identifier2 … node_identifiernumber_of_roads
or
node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.

Output

The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following:

Sample Input

4
0:(1) 1
1:(2) 2 3
2:(0)
3:(0)
5
3:(3) 1 4 2
1:(1) 0
2:(0)
0:(0)
4:(0)

Sample Output

1
2

题目大意

就是给出一棵树,每一个守卫能守住它的父亲和儿子,求最小需要守卫的数量。

分析

这道题可以有三种方法:贪心树形DP二分图匹配,我只实现了前两种方法,最后一种请读者自行实现。

贪心

这道题一看这个图,就知道叶子节点绝对不能设立守卫,如果设立了,就不是最优解了。多画画图,我们可以找到一种规律,如下图例子:

因为叶子节点不能设立守卫,所以我们设立在叶子节点的父亲上,设立守卫的点标为绿色,守卫能守到的范围标为蓝色。

因为前面的点已经被标了,后面的点就没有选择,所以就只能表没有标过的父亲节点。

完成。这就是基本的贪心思路。

源代码
#include<cstdio>
#include<cstring>
const int N=1501;
#define clear(a) memset(a,0,sizeof a)
int l[N],s[N],vis[N],map[N][16],n;
int getint(){
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x;
}
int dfs(int u){
    int place=0,r=0;vis[u]=1;
    for(int i=0;i<l[u];i++){
        if(vis[map[u][i]]) continue;
        r+=dfs(map[u][i]);
        if(!s[map[u][i]]) place=1;
    }
    return r+(s[u]=place);
}
int main()
{
    while(~scanf("%d",&n)){
        clear(l);
        clear(s);
        clear(vis);
        for(int i=1;i<=n;i++){
            int u=getint(),e=getint();
            while(e--){
                int v=getint();
                map[u][l[u]++]=v;
                map[v][l[v]++]=u;
            }
        }
        printf("%d\n",dfs(0));
    }
}
树形DP

直接给状态转移方程:
dp[u][0] = sum{dp[v][1]}; 代表u节点没有士兵
dp[u][1] = dp[u][1]+sum{dp[v][1],dp[v][0]}; 代表u节点有士兵

源代码
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
vector <int> f[1501];
int dp[1501][2];
int n,m,k,x;
bool vis[1501];
void dfs(int u,int fa){
    dp[u][1]=1;
    int len=f[u].size();
    for(int i=0;i<len;i++){
        int v=f[u][i];
        if(v==fa) continue;
        dfs(v,u);
        dp[u][0]+=dp[v][1];
        dp[u][1]+=min(dp[v][0],dp[v][1]);
    }
}
int main()
{
    while(~scanf("%d",&n)){
        for(int i=0;i<=n;i++)
            f[i].clear();
        memset(dp,0,sizeof dp);
        memset(vis,0,sizeof vis);
        for(int i=1;i<=n;i++){
            scanf("%d:(%d)",&m,&k);
            while(k--){
                scanf("%d",&x);
                f[m].push_back(x);
                f[x].push_back(m);
            }
        }
        dfs(0,-1);
        printf("%d\n",min(dp[0][0],dp[0][1]));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值