TELE(树形dp+背包)

2023-11-06 22:32
文章标签 dp 背包 树形 tele

本文主要是介绍TELE(树形dp+背包),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:TELE

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

题意:电视台向很多用户发送信号,每个用户有愿意出的钱,电视台经过的路线都有一定的费用,求电视台在不损失的情况下最多能给多少用户发送信号。即给一棵树,根结点表示发射塔,叶子结点表示用户,树的每一条边的权值表示传输需要的代价。

输入:第一行表示树的节点数n和用户数m,之后的n-m行第一个数k表示临近的点数,之后的数表示它临近的节点和边权,最后一行表示m个用户愿意出的钱。

思路:树形dp+背包

由于求的是最多多少用户,所以以用户为状态,设置一个二维dp数组。

dp[i][j]表示第i个结点为根结点的子树j个用户的时候最大的剩余费用。

价值有可能是负值,所以初始化数组时dp数组要初始化为负数

源代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stdio.h>
#include <iomanip>
#include <cmath>
#include <vector>
#define maxn 10005
using namespace std;
struct Tree{int to,next,v;
}tree[maxn];
int dp[maxn][maxn];
int n,m;
int head[maxn];
int num[maxn];
int a[maxn];
int sum;
void add(int a,int b,int v){tree[sum].to = b;tree[sum].v = v;tree[sum].next = head[a];head[a] = sum;sum++;
}
void tree_dp(int x){for (int i = head[x]; i != -1; i = tree[i].next){tree_dp(tree[i].to);for (int j = 0; j <= num[x]; j++)a[j] = dp[x][j];for (int j = 0; j <= num[x]; j++){for (int t = 1; t <= num[tree[i].to]; t++){dp[x][j+t] = max(dp[x][j+t],a[j] + dp[tree[i].to][t]-tree[i].v);}}num[x] += num[tree[i].to];}
}
int main(){while (scanf("%d%d",&n,&m)!=EOF){int a,b,k;sum = 0;memset(head,-1,sizeof(head));memset(num,0,sizeof(num));for (int i = 1; i <= n-m; i++){scanf("%d",&k);while (k--){scanf("%d%d",&a,&b);add(i,a,b);}}for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)dp[i][j] = -999999;//memset(dp,-9999999,sizeof(dp));for (int i = n-m+1; i <= n; i++){num[i] = 1;//每个叶子结点为一个用户scanf("%d",&dp[i][1]);}tree_dp(1);for (int i = m; i >= 0; i--){if (dp[1][i] >= 0){printf("%d\n",i);break;}}}return 0;
}


这篇关于TELE(树形dp+背包)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/359450

相关文章

poj2576(二维背包)

题意:n个人分成两组,两组人数只差小于1 , 并且体重只差最小 对于人数要求恰好装满,对于体重要求尽量多,一开始没做出来,看了下解题,按照自己的感觉写,然后a了 状态转移方程:dp[i][j] = max(dp[i][j],dp[i-1][j-c[k]]+c[k]);其中i表示人数,j表示背包容量,k表示输入的体重的 代码如下: #include<iostream>#include<

hdu2159(二维背包)

这是我的第一道二维背包题,没想到自己一下子就A了,但是代码写的比较乱,下面的代码是我有重新修改的 状态转移:dp[i][j] = max(dp[i][j], dp[i-1][j-c[z]]+v[z]); 其中dp[i][j]表示,打了i个怪物,消耗j的耐力值,所得到的最大经验值 代码如下: #include<iostream>#include<algorithm>#include<

csu(背包的变形题)

题目链接 这是一道背包的变形题目。好题呀 题意:给n个怪物,m个人,每个人的魔法消耗和魔法伤害不同,求打死所有怪物所需的魔法 #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>//#include<u>#include<map

hdu4826(三维DP)

这是一个百度之星的资格赛第四题 题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1004&cid=500 题意:从左上角的点到右上角的点,每个点只能走一遍,走的方向有三个:向上,向下,向右,求最大值。 咋一看像搜索题,先暴搜,TLE,然后剪枝,还是TLE.然后我就改方法,用DP来做,这题和普通dp相比,多个个向上

hdu1011(背包树形DP)

没有完全理解这题, m个人,攻打一个map,map的入口是1,在攻打某个结点之前要先攻打其他一个结点 dp[i][j]表示m个人攻打以第i个结点为根节点的子树得到的最优解 状态转移dp[i][ j ] = max(dp[i][j], dp[i][k]+dp[t][j-k]),其中t是i结点的子节点 代码如下: #include<iostream>#include<algorithm

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

hdu4865(概率DP)

题意:已知前一天和今天的天气概率,某天的天气概率和叶子的潮湿程度的概率,n天叶子的湿度,求n天最有可能的天气情况。 思路:概率DP,dp[i][j]表示第i天天气为j的概率,状态转移如下:dp[i][j] = max(dp[i][j, dp[i-1][k]*table2[k][j]*table1[j][col] )  代码如下: #include <stdio.h>#include

usaco 1.1 Broken Necklace(DP)

直接上代码 接触的第一道dp ps.大概的思路就是 先从左往右用一个数组在每个点记下蓝或黑的个数 再从右到左算一遍 最后取出最大的即可 核心语句在于: 如果 str[i] = 'r'  ,   rl[i]=rl[i-1]+1, bl[i]=0 如果 str[i] = 'b' ,  bl[i]=bl[i-1]+1, rl[i]=0 如果 str[i] = 'w',  bl[i]=b

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

uva 10154 DP 叠乌龟

题意: 给你几只乌龟,每只乌龟有自身的重量和力量。 每只乌龟的力量可以承受自身体重和在其上的几只乌龟的体重和内。 问最多能叠放几只乌龟。 解析: 先将乌龟按力量从小到大排列。 然后dp的时候从前往后叠,状态转移方程: dp[i][j] = dp[i - 1][j];if (dp[i - 1][j - 1] != inf && dp[i - 1][j - 1] <= t[i]