poj 3659

来源:互联网 发布:宝贝收藏软件 编辑:程序博客网 时间:2024/05/22 01:35

       这是一道树形DP的题,与之前的树形DP一样,同样对应一个图论的概念——最小支配集。

       首先,介绍一下什么是最小支配集。如果V是一个图G最小支配集,那么对于图G中的任意一个点,要么属于集合V,要么与集合V中的点相关联。在图G的所有支配集中,顶点数最少的支配集称为最小支配集。如果图是树,那么解决起来比较方便,否则没有多项式时间解法。

       然后,介绍一下状态转移方程。这道题有三个状态,分别是dp[u][0]表示点u属于支配集,dp[u][1]表示点u不属于支配集,其子节点被覆盖,且父节点u至少被一个子节点覆盖,dp[u][2]表示点u不属于支配集,其子节点被覆盖,且父节点u没有被子节点覆盖。dp[u][0] = ∑ min(  dp[v][0],min( dp[v][1] ,dp[v][2] ) );如果u没有子节点,那么dp[u][1] = INF,否则dp[u][1]=∑ min(  dp[v][0],dp[v][1]  ) + inc,如果u存在子一个节点a使得dp[a][0]>=dp[a][1],那么inc为0,否则inc =  ∑ min(  dp[v][0] - dp[v][1]  ) ;dp[u][2] = ∑ dp[v][1](其中,v为u的子节点,INF表示无穷大)。而最终的答案通过比较dp[r][0]和dp[r][1]得出,因为根节点不存在dp[r][2]状态(其中r表示根节点)。

       最后,总结一下,这三道树形DP都对应了图论的相关概念,这几个概念挺相近的,其动态规划的解法也有相似之处,还都可以贪心求解,建议要看把这三题都看了。


代码(C++):

#include <cstdlib>#include <iostream>#include <algorithm>#define MAX 10003#define INF 99999999using namespace std;struct edge{    int to;    int next;   };edge array[MAX*2];int head[MAX],dp[MAX][3];void func(int u,int p){     int i,v,tmp,inc;     bool tag;     dp[u][0]=1;     dp[u][2]=0;     tmp=0;     inc=INF;     tag=false;     for(i=head[u];i!=-1;i=array[i].next)     {         v=array[i].to;                                         if(v!=p) func(v,u);         else continue;         dp[u][0]+=min(dp[v][0],min(dp[v][1],dp[v][2]));         if(dp[v][0]<=dp[v][1])         {             tmp+=dp[v][0];             tag=true;         }else{             tmp+=dp[v][1];               inc=min(inc,dp[v][0]-dp[v][1]);         }         if(dp[v][1]==INF||dp[u][2]==INF) dp[u][2]=INF;         else dp[u][2]+=dp[v][1];             }     if(tag||inc!=INF)     {         dp[u][1]=tmp;         if(!tag) dp[u][1]+=inc;     }else dp[u][1]=INF;}int main(int argc, char *argv[]){    int n,u,v,i,c,ans;    while(scanf("%d",&n)!=EOF)    {        memset(head,-1,sizeof(head));         c=0;                             for(i=1;i<n;i++)        {            scanf("%d %d",&u,&v);            array[c].to=v;            array[c].next=head[u];            head[u]=c;            c++;            array[c].to=u;            array[c].next=head[v];            head[v]=c;            c++;        }        func(1,0);        ans=min(dp[1][0],dp[1][1]);        printf("%d\n",ans);    }     system("PAUSE");    return EXIT_SUCCESS;}

题目(http://poj.org/problem?id=3659):

Cell Phone Network
Time Limit: 1000MS Memory Limit: 65536K   

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

51 35 24 33 5

Sample Output

2

0 0