POJ1155TELE(트리 DP)

3702 단어 dp
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
  :dp[p][m]:    p  m user       。
dp[p][m]=max(dp[p][m],dp[p][m-k]+dp[son][k]-cost);
              0,      user    。
#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;
#define INF -9999999
typedef struct nnn
{
    int id,cost;
}node;
vector<node>map[3005];
int dp[3005][3005],val[3005],k[3005],vist[3005],N,M;
int max(int a,int b)
{
    return a>b?a:b;
}
void dfs(int p)
{
    vist[p]=1; k[p]=0;
    for(int i=0;i<=N;i++)
    dp[p][i]=INF;
    dp[p][0]=0;
    if(p>N-M) dp[p][1]=val[p],k[p]=1;//      
    for(int i=0;i<map[p].size();i++)
    {
        int son=map[p][i].id;
        if(vist[son])continue ;
        dfs(son); k[p]+=k[son];

        for(int m=k[p];m>=1;m--)
        for(int tk=1;tk<=k[son];tk++)
        dp[p][m]=max(dp[p][m],dp[p][m-tk]+dp[son][tk]-map[p][i].cost);
    }
}
int main()
{
    node p;
    int id,cost,tk;
    while(scanf("%d%d",&N,&M)>0)
    {
        for(int i=1;i<=N;i++)
        map[i].clear(),vist[i]=0;
        for(int i=1;i<=N;i++)
        if(i<=N-M)
        {
            scanf("%d",&tk);
            while(tk--)
            {
                scanf("%d%d",&id,&cost);

              p.id=id; p.cost=cost;  map[i].push_back(p);
              p.id=i; p.cost=cost; map[id].push_back(p);
            }
        }
        else{
             scanf("%d",&val[i]);
        }

        dfs(1);
        for(int i=N;i>=0;i--)
        if(dp[1][i]>=0)
        {
            printf("%d
",i);break; } } }

좋은 웹페이지 즐겨찾기