poj1155 TELE(树形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

分析:
画出一棵树来研究一下:
这里写图片描述

这就让我想起了这道题

于是我又开始yy了
f[i][j]表示选择节点i,ta及ta的子树中的价值为j时最大的被选择节点数
枚举j,那么i的儿子们就可以自由的分配j,但是j的范围我们完全无法掌控
显然这种状态不行

实际上我们不用特意去卡这个>=0的限制,
我们可以直接找到最大值
最后在寻找答案的时候只要扫一下 f[1][j] 就可以了

f[i][j]表示i这颗子树中,选择j个节点得到的最大利润
size[i]表示i这颗子树中所有用户的数量
每次枚举i的一个儿子中选取的节点数量
进行转移:f[i][j]=max{f[i][j-k]+f[son][k]-way[i].v}

然而

不能就这么simple的用f转移了
我们需要提前记录下选完上一个儿子后的状态
更新只能是在上一个儿子的基础上,而不能是当前的儿子的选择不断的叠加
有一点像背包的思想

tip

这种转移需要另一个数组记录的情况
以前也做到过,以后都要注意
如果一个状态可以从多个状态转移而来,
然而这些状态又不能重叠
这个时候就要考虑用另一个数组记录一下

这里写代码片
#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

const int O=1000;
const int N=3010;
struct node{
    int x,y,v,nxt;
};
node way[N<<1];
int n,m,pay[N],st[N],tot=0;
int f[N][N],size[N],g[N],l[N];

void add(int u,int w,int z)
{
    tot++;
    way[tot].x=u;way[tot].y=w;way[tot].v=z;way[tot].nxt=st[u];st[u]=tot;
}

void dfs(int now)
{
    if (now>n-m)
    {
        size[now]=1;
        return;
    }
    for (int i=st[now];i;i=way[i].nxt)
    {       
        dfs(way[i].y);
        size[now]+=size[way[i].y];
    }
}

void doit(int now)
{
    if (now>n-m)
    {
        f[now][0]=0;
        f[now][1]=pay[now];
        return;
    }
    f[now][0]=0;
    for (int i=st[now];i;i=way[i].nxt)
    {
        int y=way[i].y;
        doit(y);
        for (int k=size[now];k>=0;k--) l[k]=f[now][k];  //先提前记录下选完上一个儿子后的状态
        for (int k=size[y];k>=0;k--)
        {
            for (int j=size[now];j>=0;j--) g[j]=l[j];
            //更新只能是在上一个儿子的基础上,而不能是当前的儿子的选择不断的叠加  
            for (int j=size[now];j>=k;j--) g[j]=max(g[j],g[j-k]+f[y][k]-way[i].v);
            for (int j=size[now];j>=0;j--) f[now][j]=max(f[now][j],g[j]);
        }
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n-m;i++)
    {
        int k;
        scanf("%d",&k);
        for (int j=1;j<=k;j++)
        {
            int u,z;
            scanf("%d%d",&u,&z);
            add(i,u,z);
        }
    }
    for (int i=1;i<=m;i++) scanf("%d",&pay[n-m+i]);
    dfs(1);
    memset(f,128,sizeof(f));
    memset(g,128,sizeof(g));
    doit(1);
    for (int i=size[1];i>=0;i--) 
        if (f[1][i]>=0)
        {
            printf("%d",i);
            break;
        }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 给出一个$n\times m$的矩阵,每个位置上有一个非负整数,代表这个位置的海拔高度。一开始时,有一个人站在其中一个位置上。这个人可以向上、下、左、右四个方向移动,但是只能移动到海拔高度比当前位置低或者相等的位置上。一次移动只能移动一个单位长度。定义一个位置为“山顶”,当且仅当从这个位置开始移动,可以一直走到海拔高度比它低的位置上。请问,这个矩阵中最多有多少个“山顶”? 输入格式 第一行两个整数,分别表示$n$和$m$。 接下来$n$行,每行$m$个整数,表示整个矩阵。 输出格式 输出一个整数,表示最多有多少个“山顶”。 样例输入 4 4 3 2 1 4 2 3 4 3 5 6 7 8 4 5 6 7 样例输出 5 算法1 (递归dp) $O(nm)$ 对于这道题,我们可以使用递归DP来解决,用$f(i,j)$表示以$(i,j)$为起点的路径最大长度,那么最后的答案就是所有$f(i,j)$中的最大值。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码 算法2 (动态规划) $O(nm)$ 动态规划的思路与递归DP类似,只不过转移方程和实现方式有所不同。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值