POJ1155 TELE 树形DP

本文探讨了一个复杂的树状DP问题,涉及到电视台如何在不亏损的情况下,通过优化信号传输路径,最大化观众数量。文章详细介绍了问题背景、解决思路及算法实现,包括状态定义、转移方程和关键注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

               

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 63 2 2 3 2 9 32 4 2 5 23 6 2 7 2 8 24 3 3 3 1 1

Sample Output

5
 
题意:电视台发送信号给很多用户,每个用户有愿意出的钱,电视台经过的路线都有一定费用,求电视台不损失的情况下最多给多少用户发送信号。
思路:树状dp。由于求的是最多多少用户,那么我们可以把用户个数当成一个状态。dp[i][j]代表i节点为根节点的子树j个用户的时候最大剩余费用。

     则dp[i][j] = max(dp[i][j], dp[i][k]+dp[son][j-k]-w[i][son]);

    注意两点,第一点是上面式子中的dp[i][k]必须先用一个tem[MAX]数组提取出来,因为在计算的过程中会相互影响。第二点是价值可能是负值,所以dp初始化的时候要初始化为负的最大值。

 
 

 
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;struct node{    int now,val,next;} tree[9005];int n,m,len = 0;int num[3005],head[3005],dp[3005][3005],tem[3005];void add(int i,int x,int y){    tree[len].now = x;    tree[len].val = y;    tree[len].next = head[i];    head[i] = len++;}void dfs(int root){    int i,j,k,p;    for(i = head[root]; i!=-1; i=tree[i].next)    {        p = tree[i].now;        dfs(p);        for(j = 0; j<=num[root]; j++)            tem[j] = dp[root][j];        for(j = 0; j<=num[root]; j++)        {            for(k = 1; k<=num[p]; k++)                dp[root][k+j] = max(dp[root][j+k],tem[j]+dp[p][k]-tree[i].val);        }        num[root]+=num[p];    }}int main(){    int i,j,k,a,b;    while(~scanf("%d%d",&n,&m))    {        memset(head,-1,sizeof(head));        for(i = 1; i<=n-m; i++)        {            scanf("%d",&k);            num[i] = 0;            for(j = 0; j<k; j++)            {                scanf("%d%d",&a,&b);                add(i,a,b);            }        }        for(i = 1; i<=n; i++)        {            for(j = 1; j<=m; j++)                dp[i][j] = -10000000;        }        for(i = n-m+1; i<=n; i++)        {            num[i] = 1;            scanf("%d",&dp[i][1]);        }        dfs(1);        for(i = m; i>=0; i--)        {            if(dp[1][i]>=0)            {                printf("%d\n",i);                break;            }        }    }    return 0;}

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值