概述
1018 Communication System
描述
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 § is the sum of the prices of all chosen devices. Our goal is to choose a manufacturer for each device to maximize B/P.
输入
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.
输出
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.
样例输入
1 3
3 100 25 150 35 80 25
2 120 80 155 40
2 100 100 120 110
样例输出
0.649
Code
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#pragma warning(disable:4996)
using namespace std;
int main()
{
int tNum;
scanf("%d", &tNum);
while (tNum--)
{
int n;
scanf("%d", &n);
vector<vector<int>> band(n);
vector<vector<int>> price(n);
for (int i = 0; i < n; i++)
{
int m; scanf("%d", &m);
band[i].resize(m);
price[i].resize(m);
for (int j = 0; j < m; j++)
scanf("%d%d", &band[i][j], &price[i][j]);
}
vector<unordered_map<int, int>> dp(n);
for (int i = 0; i < band[0].size(); i++)
dp[0][band[0][i]] = price[0][i];
for (int i = 1; i < dp.size(); i++)
{
for (auto it = dp[i-1].begin(); it != dp[i-1].end(); it++)
{
for (int j = 0; j < band[i].size(); j++)
{
int minB = min(it->first, band[i][j]);
if (dp[i].count(minB)) {
dp[i][minB] = min(dp[i][minB], dp[i - 1][it->first] + price[i][j]);
}
else {
dp[i][minB] = dp[i - 1][it->first] + price[i][j];
}
}
}
}
double maxBP = 0.00;
for (auto it = dp[n - 1].begin(); it != dp[n - 1].end(); it++)
maxBP = max(it->first * 1.0 / it->second, maxBP);
printf("%.3lfn", maxBP);
}
return 0;
}
思路
https://blog.csdn.net/lyy289065406/article/details/6676781 的思路
https://blog.csdn.net/a1dark/article/details/17117289 的实现
最后
以上就是善良大地为你收集整理的POJ 1018 Communication System1018 Communication System的全部内容,希望文章能够帮你解决POJ 1018 Communication System1018 Communication System所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复