Description

We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices. For each device, we are free to choose from several manufacturers. Same devices from two manufacturers differ in their maximum bandwidths and prices. 
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

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by the input data for each test case. Each test case starts with a line containing a single integer n (1 ≤ n ≤ 100), the number of devices in the communication system, followed by n lines in the following format: the i-th line (1 ≤ i ≤ n) starts with mi (1 ≤ mi ≤ 100), the number of manufacturers for the i-th device, followed by mi pairs of positive integers in the same line, each indicating the bandwidth and the price of the device respectively, corresponding to a manufacturer.

Output

Your program should produce a single line for each test case containing a single number which is the maximum possible B/P for the test case. Round the numbers in the output to 3 digits after decimal point. 

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)的更多相关文章

  1. POJ 1018 Communication System(DP)

    http://poj.org/problem?id=1018 题意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产,而每个厂家生产 ...

  2. POJ 1018 Communication System (动态规划)

    We have received an order from Pizoor Communications Inc. for a special communication system. The sy ...

  3. poj 1018 Communication System

    点击打开链接 Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21007   Acc ...

  4. poj 1018 Communication System 枚举 VS 贪心

    Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21631   Accepted:  ...

  5. POJ 1018 Communication System(贪心)

    Description We have received an order from Pizoor Communications Inc. for a special communication sy ...

  6. poj 1018 Communication System (枚举)

    Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22380   Accepted:  ...

  7. POJ 1018 Communication System 贪心+枚举

    看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...

  8. POJ 1018 Communication System 题解

    本题一看似乎是递归回溯剪枝的方法.我一提交,结果超时. 然后又好像是使用DP,还可能我剪枝不够. 想了非常久,无奈忍不住偷看了下提示.发现方法真多.有贪心,DP,有高级剪枝的.还有三分法的.八仙过海各 ...

  9. poj 2324 Anniversary party(树形DP)

    /*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...

随机推荐

  1. python2 除法保留两位小数

  2. Qt学习之路2---窗口组件及窗口类型

    窗口组件: 图形用户界面由不同的窗口和窗口组件构成: Qt以组件对象的方式,构建图形用户界面. 组件的类型包括: ---容器类(父组件):用于包含其他的界面组件 ---功能类(子组件):用于实现特定的 ...

  3. R-tree 一种空间搜索的动态索引结构

    译林:R-tree 一种空间搜索的动态索引结构Antonm Guttman 摘要为了有效地处理空间数据,正如在计算机辅助设计和地理数据应用中所要求的那样,数据库需要一种索引机制能根据它们的空间位置快速 ...

  4. ContikiMAC RDC协议

    http://www.mamicode.com/info-detail-1348767.html

  5. ubuntu开启sshd服务(转)

    SSH分客户端openssh-client和openssh-server如果你只是想登陆别的机器的SSH只需要安装openssh-client(ubuntu有默认安装,如果没有则sudo apt-ge ...

  6. HTML(三):表单元素

    表单元素概述 表单(Form),用于收集用户信息.提交用户请求等处理过程        1.设计表单,并放入一些输入域        2.网站访问者在自己的计算机上填写上述输入域,并提交到服务器端   ...

  7. HTML(一):HTML基本元素标签

    一.什么是HTML HTML(Hypertext Markup Language):即超文本标记语言,是一种用来设计网页的标记语言,用该语言编写的文件,以.html或.htm为后缀,并且由浏览器解释执 ...

  8. SSM 整合

    --- 分为三层: DAO层:负责与数据源进行交互 Service:业务处理层,也可称为服务层,对上层提供统一接口的服务. Controller: 控制器层,负责处理来自客户端的请求. 通用配置: d ...

  9. 16 款最流行的JavaScript 框架

    1. jQuery – Javascript框架 jQuery 是最流行的 JavaScript 框架,它简化了HTML 文档遍历.事件处理.动画和Ajax交互.jQuery插件非常之多. 2. Do ...

  10. hadoop集群运行dedup实现去重功能

    一.配置开发环境1.我们用到的IDE是eclipse.要用它进行hadoop编程,要给eclipse安装hadoop自带的插件.(有的版本以源码提供插件,需要用户根据需要自己编译)2.用到的eclip ...