poj 1463 Strategic game(树dp)

来源:互联网 发布:金十网数据官网 编辑:程序博客网 时间:2024/06/05 15:25
Strategic game
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 5605 Accepted: 2551

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

40:(1) 11:(2) 2 32:(0)3:(0)53:(3) 1 4 21:(1) 02:(0)0:(0)4:(0)

Sample Output

12
题意:给出一棵树,在某个节点上放置一个卫兵可以管辖周围所有与该节点相连的边,问最少需要放置多少个卫兵,能管辖所有的边。
思路:设dp[i][0],表示在结点 i 没放置士兵的情况下,看住以结点 i 为根的子树的所有边所需的最少士兵;

   设dp[i][1],表示在结点 i 放置士兵的情况下,看住以结点 i 为根的子树的所有边所需的最少士兵。

状态转移方程: dp[i][0]=dp[i][0]+∑dp[j][1],j 是 i 的儿子结点;(根结点不放士兵时,与其相连的边必须由儿子结点来看守)

      dp[i][1]=dp[i][1]+∑( MIN ( dp[j][0] , dp[j][1] ) ),j 是 i 的儿子结点。 (根结点放士兵时,儿子结点可放可不放)

AC代码:

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <vector>#include <cmath>#include <cstdlib>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define ll long longusing namespace std;const int maxn=1505;struct node{    int v,next;}edge[maxn*2];int head[maxn],dp[maxn][2];bool vis[maxn];int n,num;void init(){    memset(head,-1,sizeof(head));    num=0;}void add(int u,int v){    edge[num].v=v;    edge[num].next=head[u];    head[u]=num++;}void input(){    int u,v,k;    for(int i=0;i<n;i++)    {    scanf("%d:(%d)",&u,&k);    while(k--)    {        scanf("%d",&v);        add(u,v);        add(v,u);    }    }}void dfs(int u){    vis[u]=true;    dp[u][0]=0;    dp[u][1]=1;    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].v;        if(!vis[v])        {            dfs(v);            dp[u][0]+=dp[v][1];            dp[u][1]+=min(dp[v][1],dp[v][0]);        }    }}void solve(){    memset(vis,false,sizeof(vis));    dfs(0);    printf("%d\n",min(dp[0][0],dp[0][1]));}int main(){    while(~scanf("%d",&n))    {        init();        input();        solve();    }    return 0;}


原创粉丝点击