poj 1155 TELE 基础题,树形背包

TELE
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4398 Accepted: 2387

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

Source



题目描述:

有一个电视台要用电视网络转播节目。这种电视网络是一树,树的节点为中转站或者用户。树节点的编号为1~N,其中1为总站,2~(N-M)为中转站,(总站和中转站统称为转发站)N-M+1~N为用户,电视节目从一个地方传到另一个地方都要费用,同时每一个用户愿意出相应的钱来付电视节目。现在的问题是,在电视台不亏本的前提下,要你求最多允许有多少个用户可以看到电视节目。


简而言之:dp[x][num]表示以x结点的子树,连接num个观众,所亏的最小值。树形背包即可。


 

输入:

N M N表示转发站和用户总数,M为用户数

以下N-M行,第i行第一个K,表示转发站i和K个(转发站或用户)相连, 其后第j对数val,cost表示,第i个转发站到val有边,费用cost.

最后一行M个数表示每个用户愿意负的钱。

输出:

不亏本前提下,可以收到节目最多的用户数。

(如果某个用户要收到节目(叶子结点),那么电视台到该用户的路径节点的费用都要付)






树形结构的时间复杂度特别难分析,退化了的树就成了一条链。
这个题,说实话,分析不清复杂度,但还是值得一试,1A。

题解:

http://blog.csdn.net/woshi250hua/article/details/7635680

说下核心思路:
既然有消费,那就要想到背包啊。

经典的消费与获得模型啊!!!

再来进一步分析:
有三个重要参数:
花费,收益,观众人数。

一般来说花费与背包问题中的体积有关。

但也有把“价值”当作体积的时候,因为价值太大,或者是浮点数不好处理精度。

做此题的时候,经验和直觉告诉我 观众人数应该作为“体积”。

然后dp[x][num] 表示x为根节点的子树,当连接num个观众时的最小花费。(所以花费和收益想加减,可以表示了)

一般来说,树上的动态规划,是以子树考虑的,状态的表示要联系上子树。

递归解决,先解决子树,再解决当前点。

 dp[x][v]=min(dp[x][v],w+dp[x][v-i]+dp[y][i]);(x是当前处理的结点,y代表某个子节点(枚举第一重循环),v代表人数(枚举第二重),i代表  枚举时 子节点的人数 (枚举第三重)            )。

(其实就是分组背包啦! 吐舌头



下面的代码中void dfs(int x)  void getdpn(int x)
两个函数其实可以放到一个函数里面。


#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI (4.0*atan(1.0))
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   ind<<1,le,mid
#define rson    ind<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second
#define ysk(x)  (1<<(x))
using namespace std;
//const int INF=    ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxn=3003    ;
//const int maxm=    ;
int dp[maxn][maxn];
int val[maxn];
int dpn[maxn];
int n,m;
struct Edge
{
    int from, to, w;
    Edge(){}
    Edge(int from,int to,int w):from(from),to(to),w(w){}

};
vector<Edge>edges;
vector<int> G[maxn];
void init()
{
    for(int i=1;i<=n;i++) G[i].clear();
    edges.clear();
    memset(val,0,(n+1)*sizeof val[0]);
}

bool is(int x)
{
    return   n-m+1<=x&&x<=n;
}

void dfs(int x)
{
    int up=dpn[x];
    memset(dp[x],0x3f,(n+1)*sizeof dp[x][0]);
    dp[x][0]=0;
    if(is(x))  dp[x][0]=0,dp[x][1]=-val[x];

    for(int ii=0;ii<G[x].size();ii++)
    {
        Edge & e=edges[G[x][ii]];int y=e.to; int w=e.w;
        dfs(y);
        for(int v=up;v>=0;v--)
        {
            for(int i=0;i<=dpn[y];i++)  if(v-i>=0)
            {
                dp[x][v]=min(dp[x][v],w+dp[x][v-i]+dp[y][i]);
            }
        }

    }
}


void getdpn(int x)
{

      dpn[x]=is(x)?1:0;
     for(int i=0;i<G[x].size();i++)
    {
        Edge & e=edges[G[x][i]];int y=e.to;
       getdpn(y);
       dpn[x]+=dpn[y];
    }
}
int main()
{
   while(~scanf("%d%d",&n,&m))
   {
       int num,x,y,w;
       init();
       for(int x=1;x<=n-m;x++)
       {
           scanf("%d",&num);
           for(int i=1;i<=num;i++)
           {
               scanf("%d%d",&y,&w);
               edges.push_back(Edge(x,y,w));
               int m=edges.size();
               G[x].push_back(m-1);
           }
       }
        for(int i=n-m+1;i<=n;i++)
        {
               scanf("%d",&val[i]);
        }
       getdpn(1);
       dfs(1);

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

   }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值