zoukankan      html  css  js  c++  java
  • poj 1155 TELE

    树形dp:
    题意:电视台发送信号给很多用户,每个用户有愿意出的钱,电视台经过的路线都有一定费用,求电视台不损失的情况下最多给多少用户发送信号。
    思路:树状dp。由于求的是最多多少用户,那么我们可以把用户个数当成一个状态。dp[i][j]代表i节点为根节点的子树j个用户的时候最大剩余费用。

         则dp[i][j] = max(dp[i][j], dp[i][k]+dp[son][j-k]-w[i][son]);

        注意两点,第一点是上面式子中的dp[i][k]必须先用一个tem[MAX]数组提取出来,因为在计算的过程中会相互影响。第二点是价值可能是负值,所以dp初始化的时候要初始化为负的最大值。

    head数组是头结点。tree【i】。next是邻接点。

    搞清状态方程所代表的意识。注意tem数组的用法。不用就结果出错了。

     TELE
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 3479   Accepted: 1802

    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个用户的时候最大剩余费用。
    //则dp[i][j] = max(dp[i][j], dp[i][k]+dp[son][j-k]-w[i][son]);
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    struct node
    {
        int now;
        int next;
        int val;
    }tree[9005];
    int dp[3005][3005],head[3005],tem[3005];
    int n,m,len,num[3005];
    void add(int x,int y,int v)
    {
        tree[len].now=y;
        tree[len].val=v;
        tree[len].next=head[x];
        head[x]=len++;
    }
    void dfs(int root)
    {
        int i,j,k,son;
        for(i=head[root];i!=-1;i=tree[i].next)
        {
            son=tree[i].now;
            dfs(son);
            for(j = 0; j<=num[root]; j++)
            tem[j] = dp[root][j];
            for(j = 0; j<=num[root]; j++)
            {
                for(k = 1; k<=num[son]; k++)
                    dp[root][j+k] = max(dp[root][j+k],tem[j]+dp[son][k]-tree[i].val);
            }
             num[root]+=num[son];
        }
    }
    int main()
    {
        int i,k,j,a,b;
        while(~scanf("%d%d",&n,&m))
        {
          memset(head,-1,sizeof(num));
          memset(dp,0,sizeof(dp));
            len=0;
        for(i=1;i<=n-m;i++)
        {
            cin>>k;
            num[i]=0;
            for(j=0;j<k;j++)
            {
                cin>>a>>b;
                add(i,a,b);
            }
        }
        for(i=1;i<=n;i++)
          for(j=1;j<=m;j++)
             dp[i][j]=-999999;
         for(i=n-m+1;i<=n;i++)
         {
             num[i]=1;
             cin>>dp[i][1];
         }
         dfs(1);
         for(i = m; i>=0; i--)
            {
                if(dp[1][i]>=0)
                {
                    printf("%d
    ",i);
                    break;
                }
            }
            return 0;
        }
    }
    
  • 相关阅读:
    解决执行maven项目出现 SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”. error
    linux yum命令详解
    解决 jenkins 下使用 HTML Publisher 插件后查看 html 报告显示不正常
    Centos6.5上安装sonarqube6.7.6
    Linux下解决高并发socket最大连接数限制,tcp默认1024个连接
    Linux 系统下安装 mysql5.7.25(glibc版)
    高版本sonar安装遇到的坑-sonar 6.7.5
    接口自动化测试框架 :APIAutoTest框架
    Netstat 的 10 个基本用法
    互联网测试开发面试题集锦(二)
  • 原文地址:https://www.cnblogs.com/cancangood/p/3636674.html
Copyright © 2011-2022 走看看