zoukankan      html  css  js  c++  java
  • POJ 1018 Communication System(树形DP)

    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
    ", cal());
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    webview 缓存
    winform 托盘
    winform自动保存属性变更
    序列化条件
    System.Web.HttpUtility VS System.Net.WebUtility
    javascript模拟sleep
    C#实现把科学计数法(E)转化为正常数字值 (转)
    appium远程连手机调控
    adb通过wifi连接android设备
    在项目中接口测试时怎么做的?
  • 原文地址:https://www.cnblogs.com/xinsheng/p/3428402.html
Copyright © 2011-2022 走看看