zoukankan      html  css  js  c++  java
  • poj1155 TELE

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4314   Accepted: 2337

    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

    这题是树形背包问题,可以用dp[i][j]表示以节点i为根节点的子树下,有j个人收看的最大利润。可以把每一个子节点看做一组背包,然后对没组背包进行01背包,转移方程为dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]-e[h].value  );


    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    #define inf 99999999
    #define maxn 3005
    int first[maxn],vis[maxn];
    struct node{
        int next,to,value;
    }e[2*maxn];
    int value[maxn],dp[maxn][maxn],num[maxn],w[maxn];
    int n,m;
    
    void dfs(int u)
    {
        int i,j,v,k,h;
        vis[u]=1;
        if(u>=n-m+1){   //说明是叶子节点,所以要特别处理一下
            dp[u][1]=w[u];
            dp[u][0]=0;
            num[u]=1;
            return;
        }
        for(h=first[u];h!=-1;h=e[h].next){
            v=e[h].to;
            if(vis[v])continue;
            dfs(v);
            for(j=m;j>=0;j--){
                for(k=num[v];k>=0;k--){
                    if(j-k>=0)
                    dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]-e[h].value  );
                }
            }
            num[u]+=num[v];
        }
    }
    
    
    int main()
    {
        int i,j,T,tot,u,v,k,c,d;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            tot=0;
            memset(first,-1,sizeof(first));
            for(i=1;i<=n-m;i++){
                scanf("%d",&k);
                for(j=1;j<=k;j++){
                    scanf("%d%d",&c,&d);
                    u=i;v=c;
                    tot++;
                    e[tot].next=first[u];e[tot].to=v;e[tot].value=d;
                    first[u]=tot;
    
                    tot++;
                    e[tot].next=first[v];e[tot].to=u;e[tot].value=d;
                    first[v]=tot;
    
                }
    
            }
            for(i=1;i<=n;i++){
                dp[i][0]=0;   //这里是关键点,要初始化为0
                for(j=1;j<=m;j++){
                    dp[i][j]=-inf;
                }
            }
    
            for(i=n-m+1;i<=n;i++){
                scanf("%d",&w[i]);
            }
            memset(vis,0,sizeof(vis));
            memset(num,0,sizeof(num));
            dfs(1);
            int flag=-1;
            for(i=m;i>=0;i--){
                if(dp[1][i]>=0){
                    flag=i;break;
                }
            }
            printf("%d
    ",flag);
        }
        return 0;
    }
    


  • 相关阅读:
    JS---元素属性的操作
    JS---异常
    JS---OOP
    T-SQL---分页语句
    mui---在关闭webview之前给出提示
    mui---通过plus.webview.create创建webview并打开新页面并传参到新页面
    mui---要打开的页面loaded不自动显示,等服务器返回数据后,再做处理逻辑
    winform/timer控件/权限设置/三级联动
    winform 进程、线程
    winform 之MDI容器
  • 原文地址:https://www.cnblogs.com/herumw/p/9464631.html
Copyright © 2011-2022 走看看