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. 在Hive中执行DDL之类的SQL语句时遇到的一个问题

    在Hive中执行DDL之类的SQL语句时遇到的一个问题 作者:天齐 遇到的问题如下: hive> create table ehr_base(id string); FAILED: Execut ...

  2. upupw phpmyadmin写shell

    废话: upupw给我的感觉是一个充当了waf毁三观的垃圾程序.然而,我讨厌的程序,管理员都特别喜欢用. 你会发现直接用之前那套写shell的建表然后在值里添加shell代码写到一个路径不可以了. C ...

  3. mysql 的一点点记录

    以后再来整理. -- 查询一个学校的报修单数 SELECT s.id AS schoolId , -- 学校ID COUNT(i.id) as cntId, -- 报修单数 IFNULL(t1.noh ...

  4. linux查找系统中占用磁盘空间最大的文件

    Q:下午有一客户磁盘空间占用很大,使用df查看磁盘剩余空间很小了,客户想知道是哪些文件占满了文件. Q1:在Linux下如何查看系统占用磁盘空间最大的文件? Q2:在Linux下如何让文件夹下的文件让 ...

  5. vim同时打开多个文件进行编辑

    在A文件中用:tabedit B 就打开了B文件,然后用gt来切换进入A 或B文件中: 如果打开多个,就用 1gt ,2gt来切换至不同的文件:返回上一个文件用gT

  6. C语言 · 新建Microsoft Word文档

    算法提高 新建Microsoft Word文档   时间限制:1.0s   内存限制:256.0MB      问题描述 L正在出题,新建了一个word文档,想不好取什么名字,身旁一人惊问:“你出的题 ...

  7. Quartz 一个JOB 配置多个Trigger时注意的问题

    public class SimpleExample { public void run() throws Exception { Logger log = LoggerFactory.getLogg ...

  8. Java中上传文件和表单数据提交如何保持数据的一致性?

    学生申请学科竞赛活动,表单中有学科竞赛的申报信息和部分附件,需要做到将上传文件和表单数据提交保持一致性. 将上传文件和插入表单数据放到事务汇总去处理,由于表单的数据我们可以控制,但是上传的文档不好控制 ...

  9. 二叉树的java实现

    一.分析 一个二叉树节点有三个部分,一个是指向左子树的部分,一个是指向右子树的部分,另外一个是数据部分.可以把这个节点抽象成一个节点对象,给对象有两个节点对象属性和一个数据属性.如下图: 一个二叉树有 ...

  10. B2B(企业对企业)、B2C(企业对个人)、C2C(个人对个人)

    B2B(企业对企业).B2C(企业对个人).C2C(个人对个人)