zoukankan      html  css  js  c++  java
  • 【树形dp】TELE

    [POJ1155]TELE
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5376   Accepted: 2973

    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

     
    题目大意:有一个电视网络,每条边都有边权,每个人都愿意付一定的价格来收看电视,问电视台在不亏本的情况下最多能满足多少个用户?
    试题分析:设dp[i][j]表示i号节点选j个用户赚的钱。
         那么dp[i][j]=max(dp[i][j],dp[i->son][t]+dp[i][j-t]-Cost[i->son]*2)
         边界条件(叶子结点):dp[i][1]=val[i],dp[i][0]=0;
          最后从大到小枚举人数,找到第一个dp[1][ans]为正的即可
     
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    
    inline int read(){
    	int x=0,f=1;char c=getchar();
    	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    	return x*f;
    }
    const int MAXN=8001;
    const int INF=999999;
    int N,M;
    int Node[MAXN],Root[MAXN],Next[MAXN],Cost[MAXN];
    int cnt;
    int val[MAXN];
    int tel[MAXN];
    int dp[3001][3001];
    
    void addedge(int u,int v,int w){
    	++cnt;
    	Node[cnt]=v; Cost[cnt]=w;
    	Next[cnt]=Root[u];
    	Root[u]=cnt;
    	return ;
    }
    void dfs(int x){
    	if(x>N-M){
    		tel[x]=1;
    		dp[x][1]=val[x];
    		dp[x][0]=0;
    		return ;
    	}
    	for(int k=Root[x];k;k=Next[k]){
    		int son=Node[k];
    		dfs(son);
    	}
    	int tmp=0;dp[x][0]=0;
    	for(int k=Root[x];k;k=Next[k]){
    		int son=Node[k];
    		tmp+=tel[son];
    		for(int t=tmp;t>=1;t--){
    			for(int p=1;p<=tel[son];p++){
    			    if(p>t) break;
    			    dp[x][t]=max(dp[x][t],dp[son][p]+dp[x][t-p]-Cost[k]);
    			}
    		}
    	}
    	tel[x]=tmp;
    	return ;
    }
    
    int main(){
    	N=read(),M=read();
    	for(int i=0;i<=N;i++)
    	    for(int j=0;j<=M;j++) dp[i][j]=-INF;
    	for(int i=1;i<=N-M;i++){
    		int k=read();
    		while(k--){
    			int v=read(),w=read();
    			addedge(i,v,w);
    		}
    	}
    	for(int i=1;i<=M;i++) val[i+N-M]=read();
    	dfs(1); int ans=M;
    	while(dp[1][ans]<0) ans--;
    	printf("%d
    ",ans);
    }
  • 相关阅读:
    (4)事件处理——(1)事件处理(Handling Events)
    S/4HANA服务订单Service Order的批量创建
    如何给SAP C4C的产品主数据division配置出新的下拉选项
    为什么S/4HANA的生产订单创建后会自动release
    为什么S/4HANA的销售订单创建会触发生产订单的创建
    SAP云平台对Kubernetes的支持
    什么是SAP GUI的client
    SAPGUI系统登录页面配置的SAProuter有什么用
    SAP R/3系统的R和3分别代表什么含义,负载均衡的实现原理
    一些通过SAP ABAP代码审查得出的ABAP编程最佳实践
  • 原文地址:https://www.cnblogs.com/wxjor/p/7271103.html
Copyright © 2011-2022 走看看