Communication System
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
Source
Tehran 2002, First Iran Nationwide Internet Programming Contest
这道题目的主要解决方案是剪枝搜索方法,在网上已经有很多实现了,例如:http://blog.chinaunix.net/u3/102624/showart_2037983.html 在这里要说的作者想说的是,在网络已经出现的方法基础上,还可以对算法进行进一步的优化。
一般地,我们记录下所有设备的带宽的最大值和最小值,并且,取:
downB: 所有设备的最小值带宽的最小值;
upB: 所有设备的最大值带宽的最大值;
并搜索,[downB, upB]间的所有带宽,来求得最大的B/P; 作者所看到的,别人的算法,在这里直接进行了搜索,虽然也能通过,但是这里的确可以进一步进行优化,因为并不是[downB, upB]之间的所有整数所对应的带宽都存在于具体的设备中,即存在某些整数S,且S属于[downB, upB],但却不属于输入的带宽值,因此,在进行搜索的时候,这些值完全可以跳过,以达到节省时间的目的。加入一布尔变量bindex[INFINIT],用于表示某个带宽是否在设备中出现,若其出现,则对其搜索,若其不出现,则跳过搜索。具体的代码如下:
#include "iostream"
//#include "iomanip"
#include "stdio.h"
using namespace std;
const int INFINIT = 342289;
int main()
{
int t, n, mi[100], P, downB, upB, minB, maxB, minP;
int ibd[100][100], ipr[100][100];
int i, j, k;
bool bindex[INFINIT];
double result;
cin >> t;
while(t --)
{
downB = INFINIT;
upB = INFINIT;
memset(bindex, 0, sizeof(bool)*INFINIT);
result = 0;
cin >> n;
for(i = 0; i < n; i ++)
{
minB = INFINIT;
maxB = -INFINIT;
cin >> mi[i];
for(j = 0; j < mi[i]; j ++)
{
cin >> ibd[i][j] >> ipr[i][j];
bindex[ibd[i][j]] = true;
if(minB > ibd[i][j])
{
minB = ibd[i][j];
}
if(maxB < ibd[i][j])
{
maxB = ibd[i][j];
}
}
if(downB > minB)
{
downB = minB;
}
if(upB > maxB)
{
upB = maxB;
}
}
for(k = downB; k <= upB; k ++)
{
if(bindex[k])
{
P = 0;
for(i = 0; i < n; i ++)
{
minP = INFINIT;
//for(j = 1; j < mi[i]; j ++)
for(j = 0; j < mi[i]; j ++)
{
if(ibd[i][j] >= k && minP > ipr[i][j])
{
minP = ipr[i][j];
}
}
P += minP;
}
if(result < ((double)k)/((double)P))
{
result = ((double)k)/((double)P);
}
}
}
printf("%.3f/n", result);
}
return 0;
}
提交结果: