[POJ 1155] TELE

TELE
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3787   Accepted: 2007

Description

A TV-network plans to broadcast an important football match. Their network of transmitters and users can be represented as a tree. The root of the tree is a transmitter that emits the football match, the leaves of the tree are the potential users and other vertices in the tree are relays (transmitters).   The price of transmission of a signal from one transmitter to another or to the user is given. A price of the entire broadcast is the sum of prices of all individual signal transmissions.  Every user is ready to pay a certain amount of money to watch the match and the TV-network then decides whether or not to provide the user with the signal.  Write a program that will find the maximal number of users able to watch the match so that the TV-network's doesn't lose money from broadcasting the match.

Input

The first line of the input file contains two integers N and M, 2 <= N <= 3000, 1 <= M <= N-1, the number of vertices in the tree and the number of potential users.   The root of the tree is marked with the number 1, while other transmitters are numbered 2 to N-M and potential users are numbered N-M+1 to N.  The following N-M lines contain data about the transmitters in the following form:  K A1 C1 A2 C2 ... AK CK  Means that a transmitter transmits the signal to K transmitters or users, every one of them described by the pair of numbers A and C, the transmitter or user's number and the cost of transmitting the signal to them.  The last line contains the data about users, containing M integers representing respectively the price every one of them is willing to pay to watch the match.

Output

The first and the only line of the output file should contain the maximal number of users described in the above text.

Sample Input

9 6

3 2 2 3 2 9 3

2 4 2 5 2

3 6 2 7 2 8 2

4 3 3 3 1 1

Sample Output

5

Source

Croatia OI 2002 Final Exam - Second Day
 
树形背包、注意理解、上代码 - -
#include <iostream>

#include <cstdio>

#include <cstring>

#include <queue>

#include <cmath>

#include <cstdlib>

#include <vector>

#include <map>

#include <algorithm>

using namespace std;

#define INF 0x7ffffff

#define ll long long

#define N 3010



struct Edge

{

    int to,next,len;

}edge[N];

int head[N],tot;



int n,m;

int val[N];

int num[N];

int dp[N][N]; /* dp[i][j]表示在节点i为根节点的子树,有j个叶子节点时的最大利润  */

              /* 状态转移方程: dp[i][j] = max(dp[i][j], dp[i][k]+dp[son][j-k]-w[i][son]); */

void init()

{

    tot=0;

    memset(head,-1,sizeof(head));

    for(int i=1;i<=n;i++)

    {

        for(int j=1;j<=m;j++)

        {

            dp[i][j]=-INF;

        }

    }

}

void add(int x,int y,int z)

{

    edge[tot].to=y;

    edge[tot].len=z;

    edge[tot].next=head[x];

    head[x]=tot++;

}

void dfs(int u)

{

    if(head[u]==-1)

    {

        num[u]=1;

        dp[u][1]=val[u];

    }

    for(int i=head[u];i!=-1;i=edge[i].next)

    {

        int v=edge[i].to;

        int w=edge[i].len;

        dfs(v);

        num[u]+=num[v];

        for(int j=num[u];j>=0;j--)

        {

            for(int k=0;k<=j;k++)

            {

                dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]-w);

            }

        }

    }

}

int main()

{

    while(scanf("%d%d",&n,&m)!=EOF)

    {

        init();

        for(int i=1;i<=n-m;i++)

        {

            int a,b,c;

            scanf("%d",&c);

            for(int j=0;j<c;j++)

            {

                scanf("%d%d",&a,&b);

                add(i,a,b);

                

            }

        }

        for(i=n-m+1;i<=n;i++)

        {

            scanf("%d",&val[i]);

        }

        dfs(1);



        for(int i=m;i>=0;i--)

        {

            if(dp[1][i]>=0)

            {

                printf("%d\n",i);

                break;

            }

        }

    }

    return 0;

}

 

你可能感兴趣的:(poj)