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 题意 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构, ...
随机推荐
- 修改NSMutableArray中的元素时的注意事项
最近做项目遇到从文件加载数组,并对数组中的元素进行操作的问题,特意写了个Demo,记录下要注意的东西: 代码如下: NSArray *array = @["]; NSMutableArray ...
- 记录maven 整合SSM框架
一.新建maven项目 建好的项目结构如下图: 还需要做以下配置: 勾选上这两项后,就会自动生成 "src/main/java" 和 "src/main/resour ...
- QCW切割 --铁片
1.QCW切割旋转轴限位部件 --刘锦峰协助 :铁片 功率85% 最大功率100 最小功率50 脉宽0.1ms 调整焦点-0.5左右
- 运行Chromium浏览器缺少google api密钥无法登录谷歌账号的解决办法
管理员身份运行CMD,然后依次输入以下三行内容: setx GOOGLE_API_KEY "AIzaSyAUoSnO_8k-3D4-fOp-CFopA_NQAkoVCLw"setx ...
- Ubuntu访问window下的磁盘分区出现“Error mounting /dev/sda5 at/media”错误的解决方法
我装ubuntu之前,电脑上安装了windows 10,为了装ubuntu,在window 10下的磁盘工具分配了30G的磁盘空间.安装完Ubuntu之后,访问window 10的磁盘分区出现“Err ...
- asp.net mvc 自动化测试工具
好久不写文章了,一直忙在项目中. 前一阵发现公司一个项目,体积巨大.业务很复杂.基于历史原因,项目基于mvc 2迁移过来,视图大多还是aspx 作为视图承载. 控制器中的方法 更是一个比一个多. ...
- Ubuntu16.04 install android-studio-ide-162.4069837-linux
本文讲解如何在Ununtu 16.04上安装jdk.Android Sdk.Anroid Studio.Genymotion.AndroidStudio与Genymotion绑定. 由于第一次装了双系 ...
- KM算法新识
看了很多写的好的文章,但是针对代码注释来讲,这篇文章最合适. 如果人生会有很长,愿你的荣耀永不散场--wenr大牛. #include ...
- ZOJ2212 Argus 优先队列 结构体
#include <iostream> #include <string> #include <queue> using namespace std; struct ...
- 完美实现身份证校验 js正则
注意: 1.只针对18为身份证号码进行校验,现在15位的应该很少了, 2.不区分xX大小写, 3.出生年份1900-2099,每月的天数也进行相关验证(考虑的闰月的情况), 4.校验规则详见,这个写的 ...