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 题意 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构, ...
随机推荐
- Matlab入门学习(文件读写)
一.save,load >> a=[ ]; >> b=[ ] b = >> save('b.mat','a','b');%a file named b.mat wi ...
- JAVA设计模式总结之23种设计模式
上一篇总结了设计模式的六大原则<JAVA设计模式总结之六大设计原则>,这一篇,正式进入到介绍23种设计模式的归纳总结. 一.什么是设计模式 ...
- Hbase 技术细节笔记(上)
欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者:张秀云 前言 最近在跟进Hbase的相关工作,由于之前对Hbase并不怎么了解,因此系统地学习了下Hbase,为了加深对Hbase的 ...
- 51 nod 1521 一维战舰 时间复杂度O(n),同 Codeforces 567D. One-Dimensional Battle Ships 有详细注释
题目:51nod: 题目Codeforces: 题目注意到两个战舰不能挨在一起就可以了. // 每一段 struct node{ int left; // 段的左端点 int right; // 段的 ...
- Android Studio安装应用时报错 installation failed with message Failed to finalize session......
解决方法: 在AndroidManifest.xml中的provider中的authorities后加几个数字即可. 2017.09.01: 我发现有的项目AndroidManifest.xml中没有 ...
- eclipse安装lombok插件问题解决
在 java平台上,lombok 提供了简单的注解的形式来帮助我们消除一些必须有但看起来很臃肿的代码, 比如属性的get/set,及对象的toString等方法,特别是相对于 POJO.简单的说,就是 ...
- Docker入门之七Dockerfile
Dockerfile是一个文本格式的配置文本,可以使用它来创建自定义的镜像.首先我们可以先看一个dockerfile是什么样子.这里可以有一个网站不错:http://dockerfile.github ...
- JavaScript 版数据结构与算法(一)栈
今天,我们要讲的是数据结构与算法中的栈. 栈的简介 栈是什么?栈是一个后进先出(LIFO)的数据结构.栈有啥作用?栈可以模拟算法或生活中的一些后进先出的场景,比如: 十进制转二进制,你需要将余数倒序输 ...
- ASP.NET没有魔法——ASP.NET MVC 与数据库之EF实体类与数据库结构
大家都知道在关系型数据库中每张表的每个字段都会有自己的属性,如:数据类型.长度.是否为空.主外键.索引以及表与表之间的关系.但对于C#编写的类来说,它的属性只有一个数据类型和类与类之间的关系,但是在M ...
- zeroc
ZeroC ICE 是指ZeroC公司的ICE(Internet Communications Engine)中间件平台.对于客户端和服务端程序的开发提供了很大的便利. 目前ICE平台中包括Ice,I ...