poj 1155 TELE

TELE
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4544 Accepted: 2466

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
题目大意:广播电视网络计划一个重要的足球比赛。发射器和网络用户可以表示成一棵树。树的根是一个能发出足球比赛的发射机,树的叶子是潜在用户和其他顶点树中的继电器(发射器)。
传输一个信号从一个发射机发射给用户或另一个发射机需要一定的费用,即为树的边权。每个用户愿意支付一定数量的钱来观看比赛,根据收益电视网络然后决定是否向用户提供信号。
编写一个程序,求最多多少用户能够观看比赛,在广播电视网络不亏钱的前提下。

第一行输入两个数。分别表示总结点数和用户数。编号1-(n-m)表示发射机,(n-m+1)-n 表示用户。

接下来N-m 行,每行第一个数k表示有多少节点与该发射机相连,接下来k组数x,y 表示该发射机与节点x的边权为y

题解:树形dp。

刚开始毫无头绪,于是开始画树,经过YY终于想到了。

f[i][j]表i 这颗子树选择j 个用户的最大盈利

size[i]表示i这颗子树中有一共有多少个用户(注意不是选择多少)

然后枚举i 的所有儿子,和所有儿子可以选择的用户数,不断更新f[i][j]。 但是需要注意处理好同一儿子两个不同的选择不可同时被选。总之有点类似于背包。

还要注意poj 内存较小,刚开始MLE了。 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 3003
using namespace std;
int n,m;
int f[N][N],size[N],point[N],next[N],v[N],cost[N],tot,pay[N];
int g[N],l[N];
void add(int x,int y,int z)
{
  tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; cost[tot]=z;
}
void calc(int x)
{
  if (x>n-m)
   {
     size[x]=1;
     return;
   }
  for (int i=point[x];i;i=next[i])
   {
   	 calc(v[i]);
   	 size[x]+=size[v[i]];
   }
}
void build(int x)
{
  if (x>n-m)
   {
   	  f[x][1]=pay[x];
   	  f[x][0]=0;
   	  return;
   }
  f[x][0]=0;
  for (int j=point[x];j;j=next[j])
    {
      build(v[j]);
      for (int k=size[x];k>=0;k--)  l[k]=f[x][k];//先提前记录下选完上一个儿子后的状态
      for (int i=size[v[j]];i>=1;i--)
       {
       for (int k=size[x];k>=0;k--)  g[k]=l[k];//更新只能是在上一个儿子的基础上,而不能是当前的儿子的选择不断的叠加
       for (int k=size[x];k>=i;k--)
        g[k]=max(g[k],g[k-i]+f[v[j]][i]-cost[j]);
       for (int k=size[x];k>=0;k--)  f[x][k]=max(f[x][k],g[k]);
       }
    }
}
int main()
{
  scanf("%d%d",&n,&m);
  for (int i=1;i<=n-m;i++)
   {
    int x; scanf("%d",&x);
    for (int j=1;j<=x;j++)
     {
      int a,b; scanf("%d%d",&a,&b);
      add(i,a,b);
     }
   }
  for (int i=1;i<=m;i++)
   scanf("%d",&pay[n-m+i]);
  calc(1);
  memset(f,128,sizeof(f));
  memset(g,128,sizeof(g));
  build(1);
  for (int i=size[1];i>=0;i--)
   if (f[1][i]>=0)
   { 
    printf("%d\n",i);
    return 0;
   }
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值