POJ 1155 TELE 樹形背包,pojtele

來源:互聯網
上載者:User

POJ 1155 TELE 樹形背包,pojtele

連結:http://poj.org/problem?id=1155

題意:電視台轉播一場重要的足球比賽,以這個轉播機器為根建立一棵“轉播樹”,數中一共有N個(N<=3000),葉子節點是所有使用者,有M個使用者(M<=N-1),每個使用者有固定的付費額度。除了使用者以外的結點都是轉接訊號的機器,在所有結點之間連線都需要有一定的花費。現在要求我儘可能滿足更多人的觀看要求,並且保證轉播不賠錢。問最多可以保證多少人的觀看要求。

思路:從葉到根依次記錄當前結點可以滿足的觀看要求的數量,並且記錄滿足該數量的最多收益,即對每個結點進行背包DP,保證每次決策的最佳化。

狀態轉移方程:dp[u][j]=max(dp[u][j],dp[v][k]+dp[u][j-k]-w),u是當前結點,v是子結點,j是u的一種可以滿足的人數,k是已經從v的一種可以滿足的人數,dp[v][k]已經先確定過了。w是路徑的花費值。對於每個葉子結點dp[u][1]就是u可以付的費用。

代碼:

#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#include <ctype.h>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x3fffffff#define maxn 3005#define PI acos(-1.0)#define seed 31//131,1313typedef long long LL;typedef unsigned long long ULL;using namespace std;int head[maxn],top,dp[maxn][maxn],t[maxn],N,M,tot,x,y,num[maxn];void init(){    memset(head,-1,sizeof(head));    for(int i=1; i<=N; i++)        for(int j=1; j<=N; j++)            dp[i][j]=-INF;    memset(t,0,sizeof(t));    memset(num,0,sizeof(num));    top=0;}struct Edge{    int v,w;    int next;} edge[maxn];void add_edge(int u,int v,int w){    edge[top].v=v;    edge[top].w=w;    edge[top].next=head[u];    head[u]=top++;}void dfs(int u,int f){    for(int i=head[u]; i!=-1; i=edge[i].next)    {        int v=edge[i].v,w=edge[i].w;        if(v!=f)        {            dfs(v,u);            num[u]+=num[v];            for(int j=num[u]; j>=1; j--)                for(int k=1; k<=min(j,num[v]); k++)                    if(dp[v][k]!=-INF)                        dp[u][j]=max(dp[u][j],dp[v][k]+dp[u][j-k]-w);        }    }}int main(){    scanf("%d%d",&N,&M);    init();    for(int i=1; i<=N-M; i++)    {        scanf("%d",&tot);        for(int j=1; j<=tot; j++)        {            scanf("%d%d",&x,&y);            add_edge(i,x,y);        }    }    for(int i=N-M+1; i<=N; i++)    {        scanf("%d",&dp[i][1]);        num[i]=1;    }    dfs(1,1);    for(int i=num[1]; i>=0; i--)        if(dp[1][i]>=0)        {            printf("%d\n",i);            break;        }    return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.