程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ1155TELE (樹形DP)

POJ1155TELE (樹形DP)

編輯:C++入門知識

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
#include
#include
using namespace std;
#define INF -9999999
typedef struct nnn
{
    int id,cost;
}node;
vectormap[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=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\n",i);break;
        }
    }
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved