POJ 1018 Communication System(树形DP)
Description
By overall bandwidth (B) we mean the minimum of the bandwidths of the chosen devices in the communication system and the total price (P) is the sum of the prices of all chosen devices. Our goal is to choose a manufacturer for each device to maximize B/P.
Input
Output
Sample Input
1 3
3 100 25 150 35 80 25
2 120 80 155 40
2 100 100 120 110
Sample Output
0.649
题目:
communication system 由n种设备组成, 第i种设备会有mi个厂商提供, 每个厂商提供的设备的带宽和价格不一定相同. 让你选择n种设备, 每种一件, 使得B/P最大, 其中B是这n件设备中带宽最小的值, P是n件设备的总价格
思路:
DP[i][j]表示选取前i种设备, 带宽为j时的最小花费
DP[i][j] = min(DP[i-1][s]+cost[i][1], DP[i-1][s]+cost[i][2]…DP[i-1][s]+cost[i][m]) s>=j, 第i种物品由m个工厂提供, cost[i][k]是第i种设备, 第k个厂商的报价
递归的写法
DP[i][j] = min(DP[i][j], DP[i-1][s]+cost[i][k]) k=[1,n]
循环的次数, i, j, k, 三重循环
总结:
1. 这个DP解法又是填写二维矩阵, 不过与以往不同的是, 最终解答并没有把二维矩阵填满, 我在这个地方纠结了很久, 看着状态转移方程依然写不出代码
2. 这道DP题, 是以push的方式更新二维矩阵的值, 而不是主动 request. 从状态转移方程 DP[i][j] = min(DP[i][j], DP[i-1][s]+cost[i][k]) k=[1,n], s>=k 可以看出, for循环时, 以s为准, 但更新的是j的值. 一般的dp题目, 是j>s, 然后求解j时, 取出s对应的值即可, 称为 request
3. 打印小数点后3位 printf("%0.3f", out)
4. dp 的初始化问题, 我曾想将dp初始化为 INF, 这样的话就不需要对dp==-1进行判断, 直接用 min 就好, 于是就写了如下代码

当写到"dp[i][j] = ", 的时候发现xxx位置处不知填什么好了.
当将 dp 初始化为 INF 时, 并使用min将二维矩阵填满, 省去了判断但复杂度会比较高. 并且, 初始化的时候, 不仅需要初始化dp[0][bw[0][j]], 还需要将第一行dp[0]全部都初始化
Anyway, 还是将 dp 初始化为 -1 比较好
代码:
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 150;
vector<int> bw[MAXN];
vector<int> pc[MAXN];
int maxBW[MAXN];
int t, n, mi;
int b,p;
int dp[MAXN][1500];
double cal() {
memset(dp, -1, sizeof(dp));
for(int i = 0; i < n; i ++) {
if(i == 0) {
for(int j = 0; j < bw[i].size(); j ++) { // 第 j 个设备
int curBw = bw[0][j];
if(dp[0][curBw] == -1)
dp[0][curBw] = pc[0][j];
else
dp[0][curBw] = min(dp[0][curBw], pc[0][j]);
}
continue;
}
for(int j = 0; j <= maxBW[i-1]; j ++) {
if(dp[i-1][j] != -1) {
for(int k = 0; k < bw[i].size(); k++) {
int tb = min(j, bw[i][k]);
if(dp[i][tb] == -1)
dp[i][tb] = dp[i-1][j] + pc[i][k];
else
dp[i][tb] = min(dp[i][tb], dp[i-1][j]+pc[i][k]);
}
}
}
} double res = 0.0;
for(int i = 0; i <= maxBW[n-1]; i ++)
if(dp[n-1][i] != -1)
res = max(res, i*1.0/dp[n-1][i]);
return res; }
int main() {
//freopen("E:\\Copy\\ACM\\poj\\1018\\in.txt", "r", stdin);
cin >> t;
while(--t>=0) {
cin>>n;
for(int i = 0; i < n; i ++) {
bw[i].clear();pc[i].clear();
maxBW[i] = -1;
cin >> mi;
for(int j = 0; j < mi; j ++) {
cin >> b >> p;
bw[i].push_back(b);
pc[i].push_back(p);
maxBW[i] = max(maxBW[i], b);
}
}
// main function;
printf("%0.3f\n", cal());
}
return 0;
}
POJ 1018 Communication System(树形DP)的更多相关文章
- POJ 1018 Communication System(DP)
http://poj.org/problem?id=1018 题意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产,而每个厂家生产 ...
- POJ 1018 Communication System (动态规划)
We have received an order from Pizoor Communications Inc. for a special communication system. The sy ...
- poj 1018 Communication System
点击打开链接 Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21007 Acc ...
- poj 1018 Communication System 枚举 VS 贪心
Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21631 Accepted: ...
- POJ 1018 Communication System(贪心)
Description We have received an order from Pizoor Communications Inc. for a special communication sy ...
- poj 1018 Communication System (枚举)
Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22380 Accepted: ...
- POJ 1018 Communication System 贪心+枚举
看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...
- POJ 1018 Communication System 题解
本题一看似乎是递归回溯剪枝的方法.我一提交,结果超时. 然后又好像是使用DP,还可能我剪枝不够. 想了非常久,无奈忍不住偷看了下提示.发现方法真多.有贪心,DP,有高级剪枝的.还有三分法的.八仙过海各 ...
- poj 2324 Anniversary party(树形DP)
/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...
随机推荐
- http,javascript的编码解码
http,javascript的编码解码 请求与响应的编码应分开分析 两者的编码,解码处理是相对独立的流程 依赖于相对独立的header: request header, response heade ...
- Hadoop集群datanode死掉或者secondarynamenode进程消失处理办法
当Hadoop集群的某单个节点出现问题时,一般不必重启整个系统,只须重启这个节点,它会自动连入整个集群. 在坏死的节点上输入如下命令即可: hadoop-daemon.sh start datanod ...
- js判断字符是否存在汉字的方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Intellij IDEA创建Maven Web项目<转>
1前言 在创建项目中,IDEA提供了很多项目模板,比如Spring MVC模板,可以直接创建一个基于Maven的Spring MVC的demo,各种配置都已经设定好了,直接编译部署就可以使用. 最开始 ...
- 修改 login的串口重定向
1 在console-telnet 使用vi工具编辑 /etc/inittab 文件 vi /etc/inittab (回车)2 按 i 进入编辑模式:3 将文件中的ttyS0 改为 ttyS3 ...
- [Django学习]上传图片
上传图片 当Django在处理文件上传的时候,文件数据被保存在request.FILES FILES中的每个键为<input type="file" name="& ...
- vlan pvid vid access口 trunk口
VLAN技术浅谈 http://www.h3c.com.cn/MiniSite/H3care_Club/Data_Center/Net_Reptile/The_One/Home/Catalog/ ...
- json数据 提示框flash.now[:notice] flash.now[:alert]
实现json.做出提示框 1.在controller中使用flash.now[:alert] = "str"方法来做print def topodata #@vnic = Vnic ...
- BCM_SDK命令
启动bcm的sdk,会进入一个类似shell的交互界面,在其中如入命令,可以配置交换机芯片.本文主要记录一下命令: 1.端口限速命令 2.链路聚合命令 3.i2c控制命令 启动方法: /tmp/bcm ...
- e675. 翻转缓冲图像
// To create a buffered image, see e666 创建缓冲图像 // Flip the image vertically AffineTransform tx = Aff ...