poj 1155 TELE(树形DP)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 4863 | Accepted: 2673 |
Description
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 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
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
/*1是广播总站 ,2到n-m是重转站,n-m+1到n是用户,给出连接两个站或用户之间的费用,以及每个用户愿意出的钱,问在电视台
不亏本的前提下,最多有多少用户可以收看到电视*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <iomanip>
#define INF 0x3f3f3f3f
#define N 3010
using namespace std;
int n,m;
struct node
{
int to,w;
node(int a,int b){to=a,w=b;};
};
vector<node >edge[N*];
int val[N];
int dp[N][N];//dp[u][j]表示以u为结点,提供给j个用户的的时候最少亏的钱
int dfs(int u,int p)//当前结点,父节点
{
dp[u][]=;
int ans=,cut=;//记录用户的人数,和v是不是用户
for(int i=;i<edge[u].size();i++)
{
int v=edge[u][i].to;//下一步
int w=edge[u][i].w;//走这一步需要的费用
if(v==p) continue;//和父节点重合的时候就跳过
int cup=dfs(v,u);//v的用户人数
if(v>=n-m+)//v是用户不是广播站
{
cup++;//u的用户总人数+1
cut=;
}
else
cut=;
ans+=cup;//u的用户人数
//cout<<"cup="<<cup<<endl;
for(int j=min(ans,m);j>=;j--)//u的用户
{
for(int k=min(cup,j);k>=;k--)//v的用户
{
dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v][k-cut]+w-val[v]);
}
} }
//cout<<"u="<<u<<" "<<"ans="<<ans<<endl;
/*dfs出每结点的儿子数没问题*/
return ans;//输出有几个用户
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF)
{
int a,b,t;
for(int i=;i<=n;i++)
edge[i].clear();
memset(val,,sizeof val);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
dp[i][j]=INF;
for(int i=;i<=n-m;i++)
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&a,&b);
edge[i].push_back(node(a,b));
edge[a].push_back(node(i,b));
}
}//整理输入
for(int i=n-m+;i<=n;i++)
scanf("%d",&val[i]);
dfs(,-);
int cur=;
for(int i=;i<=n;i++)
{
//cout<<"dp[1][i]="<<dp[1][i]<<endl;
if(dp[][i]<=)
cur=i;
}
printf("%d\n",cur);
}
return ;
}
poj 1155 TELE(树形DP)的更多相关文章
- [POJ 1155] TELE (树形dp)
题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...
- POJ 1155 TELE 背包型树形DP 经典题
由电视台,中转站,和用户的电视组成的体系刚好是一棵树 n个节点,编号分别为1~n,1是电视台中心,2~n-m是中转站,n-m+1~n是用户,1为root 现在节点1准备转播一场比赛,已知从一个节点传送 ...
- Apple Tree POJ - 2486 (树形dp)
题目链接: D - 树形dp POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...
- Anniversary party POJ - 2342 (树形DP)
题目链接: POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...
- POJ 3107.Godfather 树形dp
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7536 Accepted: 2659 Descrip ...
- POJ 3342 (树形DP)
题意 :给出一些上下级关系,要求i和i的直接上级不能同时出现,现在选出一些人构成一个集合,问你这个集合里面的最大人数是都少,同时给出这个最大的人数的集合是否唯一. 思路:树形DP,dp[i][0],表 ...
- POJ 2342 (树形DP)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3863 Accepted: 2172 ...
- POJ Anniversary party 树形DP
/* 树形dp: 给一颗树,要求一组节点,节点之间没有父子关系,并且使得所有的节点的权值和最大 对于每一个节点,我们有两种状态 dp[i][0]表示不选择节点i,以节点i为根的子树所能形成的节点集所能 ...
- poj 1155 TELE (树形背包dp)
本文出自 http://blog.csdn.net/shuangde800 题目链接: poj-1155 题意 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构, ...
随机推荐
- 在http请求中的Content-Type
声明:multipart/form-data:既可以上传文件等二进制数据,也可以上传表单键值对,只是最后会转化为一条信息: x-www-form-urlencoded:只能上传键值对,并且键值对都是间 ...
- 我的Spring学习记录(四)
虽然Spring管理这我们的Bean很方便,但是,我们需要使用xml配置大量的Bean信息,告诉Spring我们要干嘛,这还是挺烦的,毕竟当我们的Bean随之增多的话,xml的各种配置会让人很头疼. ...
- HDU 1754 I Hate It(线段树区间求最值)
很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有 ...
- php里的抽象类和接口
//实例化类产生对象.//class fenbi//{// //普通成员,属于对象// public $length = "10cm";// //静态成员,静态变量,属于类.// ...
- SQL监测语句
SELECT top 20 qs.creation_time,last_execution_time,total_physical_reads,total_logical_reads,total_lo ...
- ubuntu的应用程序哪里找
一般来说11.04以前的桌面是gnome,点左上角的那个小圆圈,会出来一个下拉菜单,里面就有应用程序. 11.10以后的桌面换成unity 在dash中寻找应用程序很不方便,知道名字的还好,不知道名字 ...
- structs2的核心和工作原理
在学习struts2之前,首先我们要明白使用struts2的目的是什么?它能给我们带来什么样的好处? 设计目标 Struts设计的第一目标就是使MVC模式应用于web程序设计.在这儿MVC模式的 ...
- YYModel学习总结YYClassInfo(1)
OC的run-time 机制,简直像是网络上的猫! 我在开发中很少用到,但是作为iOS开发 人家肯定会问这个东西,所以深入的学习了下. 对于 run-time的入手,YYModel的学习,简直让人美滋 ...
- SQL SERVER 查看日志大小及日志已满的处理方法 (转)
--解决方法 --日志文件满而造成SQL数据库无法写入文件时,可用两种方法: --查看数据库日志大小 dbcc sqlperf(logspace) --清空日志. --1.打开查询分析器,输入命令 D ...
- 一道javascript面试题(闭包与函数柯里化)
要求写一个函数add(),分别实现能如下效果: (1)console.log(add(1)(2)(3)(4)()); (2)console.log(add(1,2)(3,4)()); (3)conso ...