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不去时的最大欢乐值, ...
随机推荐
- 关于Parse字符串为时间一次被坑经历
在Java代码中发现一个bug,就是本来更新为时间的内容更新为一些奇怪的内容,比如20819这种形式,本来更新的时间都是近期不会超过一年, 为什么会出现这种情况,非常奇怪,遂调试下代码,跟踪发现要匹配 ...
- LeetCode: Merge Intervals 解题报告
Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [ ...
- ehcache 缓存管理工具
ehcache ehcache.xml <ehcache> <diskStore path="java.io.tmpdir" /> <def ...
- egret请求参数
egret的文档还不错,不过http请求参数的文档居然没有,于是自己摸索了一下, 发现原来是直接传字符串参数就可以了.于是封装了一个类,方便使用. class RequestParam { publi ...
- thymeleaf 的hello world
在研究一个模板引擎,选了这个thymeleaf .中间遇到很多的问题.现在在这里记录一下. 第一步:导入jar包.这里使用maven导入jar包 <!-- thymeleaf 核心包 begin ...
- 关于github上开源nineoldandroids兼容动画的笔记
github上面有个样例非常好的列举了NineOldAndroids兼容动画的使用方法.下载了下来然后执行測试了下: 由于要引用nineoldandroids的项目,我这里直接把它的源码拷贝进去了,用 ...
- Android中WebView与H5的交互,Native与JS方法互调
项目中经常用到WebView与H5的交互,一个是H5调本地方法,一个是本地调H5方法,在此记录一下. 首先,启用JS支持 //启用js支持 webSettings.setJavaScriptEnabl ...
- 【C#】List列表的深复制,引用类型深复制
需求:深复制该列表. Student实体类: public class Student { public string Name { get; set; } public int Age { get; ...
- 文件服务器和web应用分离的思路(转)
目前在做一个应用,有不同的客户端,包括web应用的客户端,其他的客户端,都要访问我的文件服务器,通过文件服务程序提供的服务来访问文件,但是对文件管理服务器这个应用,没有什么思路,请大家给点思路,谢谢: ...
- LINQ教程三:Lambda表达式解剖
C#3.0(.NET3.5)中引入了Lambda表达式和LINQ.Lambda表达式是使用一些特殊语法表示匿名方法的较短方法. 最基本的Lambda表达式语法如下: (参数列表)=>{方法体} ...