poj 1018 Communication System (枚举+贪心)

Communication System

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 21648 Accepted: 7697

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

 

 

解题思路:

首先需要明确,要使得B/P最大,自然是要令B尽可能大,P尽可能小。

由于B和P是两个相互制约的变量,而他们又要同时取得尽可能地取极值,那么可以先令其中一个值“暂时固定”下来。

令输入的行数就是设备的种类数,即第i行描述的是第i种设备,及提供第i种设备的厂家的产品信息。

 

使用枚举+剪枝的做法:

首先B的值肯定是厂家生产的所有设备中的某个带宽值,所以可以枚举所有的带宽,每次枚举B值时,B值就被“暂时固定”了。

其次,记录所选取的B是属于第k种设备的,再从余下的设备中,选取其余n-1种设备各一个,要求所选取的设备的带宽>=B(这是由题意确定的),而价格是要满足带宽的要求下的最小值。

当n种设备都选取后,计算B/P,然后再枚举下一个B,重复上述操作。比较不同的B值所得到的B/P值,选取最大的一个就是答案。

 

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define N 20005
const int M=1005;
struct node
{
 int b,p,id;
}f[N];
int cmp(const void*a,const void*b)
{
 if((*(struct node*)a).b==(*(struct node*)b).b)        //带宽相等
 {
  if((*(struct node*)a).p==(*(struct node*)b).p)     //价格相等
   return (*(struct node*)a).id-(*(struct node*)b).id;  //以编号为第三优先升序排序   
  return (*(struct node*)a).p-(*(struct node*)b).p;
 }
 return (*(struct node*)a).b-(*(struct node*)b).b;
}
double Max(double a,double b)
{
 return a>b?a:b;
}
int main()
{
    int t,n,m,i,j;
 int visit[M];
 scanf("%d",&t);
 while(t--)
 {
  int cnt=0;
  scanf("%d",&n);
  for(i=0;i<n;i++)         //把所有信息统计到一个结构体中,cnt记录总商品数目
  {
   scanf("%d",&m);
   for(j=0;j<m;j++) 
   {
    scanf("%d%d",&f[cnt].b,&f[cnt].p);
    f[cnt++].id=i;
   }
  }
  qsort(f,cnt,sizeof(f[0]),cmp);      //按从小到大的顺序
  double ans=0,s;            //ans记录结果,s记录总价钱
  for(i=0;i<=cnt-n;i++)
  {
   memset(visit,0,sizeof(visit));
   visit[f[i].id]=1;
   s=f[i].p;
   int num=1;
   for(j=i+1;j<cnt;j++)
   {
    if(visit[f[j].id])
     continue;   
    s+=f[j].p;
    visit[f[j].id]=1;
    num++;
   }
   if(num<n)
    break;
   ans=Max(ans,f[i].b/s);
  }
  printf("%.3f\n",ans);
 }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值