POJ 1018 Communication System 枚举 暴力

  Communication System
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 16895Accepted: 5943

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

 

看题目就花了半个小时~

大概意思是 某个通信系统由m个设备组成,每个设备可以由多个厂家生产, 不同厂家的同类设备 价格和带宽不同;

要求 你选择一个带宽B 去购买设备 使得 B/sum(p)的值 最大

 

思路:

              记录下 所有设备带宽中最小的带宽值 MinB

              记录 同种类设备带宽中的最大值 并在这些最大值中选择一个最小值 MaxB

              得到了 B的 范围[MinB, MaxB];

              枚举B的取值 对每个种类  挑选一个 bi >B 且 价格最小p 的设备 得到在B下的 Sum(p);

              ans = min(B/Sum(p) );

 

 代码:

#include <iostream>
#include <algorithm>
using namespace std;
const int M = 110;
int num[M],b[M*M];
struct Device
{
	int b[M],p[M];
}d[M];
int main()
{
	int Case,t,m,i,j,l,k,MaxBi;
	scanf("%d", &Case);
	while(Case--)
	{
		int MinB, MaxB=999999;
		scanf("%d", &m);
		for(i=0,l=0; i<m; i++)
		{
			scanf("%d",&num[i]);
			MaxBi = 0;
			for(j=0; j<num[i]; j++)
			{
				scanf("%d %d", &d[i].b[j], &d[i].p[j]);
				b[l++]=d[i].b[j];
				if(MaxBi < d[i].b[j]) MaxBi = d[i].b[j];//单组最大值
			}
			if(MaxB > MaxBi) MaxB = MaxBi;//所有单组最大中的 最小
		}
		sort(b, b+l);
		int sumP=0;
		double ans = 0.0;
		for(i=0; i<l; i++)//枚举最小值
		{
			if(b[i]<=MaxB)
			{
				MinB = b[i]; sumP=0;
				for(j=0; j<m; j++)// 第几件设备
				{
					t = 9999999;
					for(k=0; k<num[j]; k++)//第j件 设备 有多少种
					{
						if(d[j].b[k]>=MinB && t>=d[j].p[k])
							t = d[j].p[k];
					}
					sumP += t;
					if((double)MinB/(double)sumP < ans) break;
				}
				if((double)MinB/(double)sumP > ans) ans = (double)MinB/(double)sumP;
			}
			else break;
		}
		printf("%.3lf\n", ans);
	}
	return 0;
}


错误:之前用t 存测试组数,但是在后面又用了t  导致不能结束。 ORZ 超时好多次 因为这题本来就容易超时 还以为算法有问题 找半天才发现

            改了提交 发现WA ,晕 优化的时候 if(MinB/sumP < ans) break 忘了强制转换 类型double; 改完在提交就AC了;

总结: 在做题的时候 不够专心,导致 经常在细节出现失误,思路不够连贯 完成代码时间太长

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值