Communication System
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21631   Accepted: 7689

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

做这题的时候,一开始的想法就是暴力枚举,虽然这样可能会超时,但是觉得可以做强有力的剪枝,于是就试了下,可是没有做论证推断,不知道应该怎样去做剪枝,其实主要是嫌麻烦,下面是我简单的枚举代码,不过超时了。

#include <stdio.h>
#include <stdlib.h>
#define MAX 100 struct Dev{
int b;
int p; }dev[MAX][MAX]; int n;
float result = -1; void Init()
{
int i, j;
for(i=0; i < MAX; i++)
{
for(j=0; j < MAX; j++)
{
dev[i][j].b= -1;
dev[i][j].p = -1;
}
}
} void PrintData(int *data, int *sum)
{
int min=data[0], temp=sum[0], i;
float tempRes;
for(i=1; i < n; i++)
{
if(data[i] < min) min = data[i];
temp += sum[i];
}
tempRes = min*1.0/ temp;
if(tempRes > result)
result = tempRes;
} void SolveCase(int *data,int *sum, int depth)
{
int i;
for(i=0; i<MAX && dev[depth][i].b != -1; i++)
{
data[depth] = dev[depth][i].b;
sum[depth] = dev[depth][i].p;
if(depth==n-1)
PrintData(data, sum);
else
SolveCase(data,sum, depth+1);
}
} int main()
{
// freopen("input.txt","r",stdin);
int caseNum, number;
int *testData, *sum , i, j; scanf("%d",&caseNum);
while(caseNum > 0)
{
Init();
scanf("%d", &n);
for(i=0; i < n; i++)
{
scanf("%d",&number);
for(j=0; j < number; j++)
scanf("%d %d", &dev[i][j].b, &dev[i][j].p);
}
testData = (int *)malloc(sizeof(int)*n);
sum = (int *)malloc(sizeof(int)*n);
SolveCase(testData,sum,0);
printf("%.3f\n",result);
result = -1;
caseNum--;
}
free(testData);
free(sum);
// fclose(stdin);
return 0;
}


超时之后,感觉可以用贪心做,然后贪心的话每次使得b 值增大,使得 p 值减少,这样才能使得结果是最大了,思路很简单,以为还是不行,结果AC 了.....

#include<cstdio>
#include<cstring>
int main()
{
// freopen("input.txt","r",stdin);
int t,n,m,b[105][105],fac[105],p[105][105],flag[32767],max,min,tp;
scanf("%d",&t);
while(t--){
max=0,min=9999999;
scanf("%d",&n);
memset(flag,0,sizeof(flag));
for(int i=0;i<n;i++){
scanf("%d",&fac[i]);
for(int j=0;j<fac[i];j++){
scanf("%d%d",&b[i][j],&p[i][j]);
flag[b[i][j]]=1;
if(max<b[i][j])
max=b[i][j];
if(min>b[i][j])
min=b[i][j];
}
}
double result=0;
for(int i=min;i<=max;i++){
if(flag[i]){
int sum=0;
for(int j=0;j<n;j++){
tp=99999999;
for(int k=0;k<fac[j];k++){
if(b[j][k]>=i&&p[j][k]<tp){
tp=p[j][k];
}
}
sum+=tp;
}
double temp=(double)i/sum;
if(result<temp)
result=temp;
}
}
printf("%.3f\n",result);
}
// fclose(stdin);
return 0;
}
												

poj 1018 Communication System 枚举 VS 贪心的更多相关文章

  1. poj 1018 Communication System (枚举)

    Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22380   Accepted:  ...

  2. POJ 1018 Communication System(贪心)

    Description We have received an order from Pizoor Communications Inc. for a special communication sy ...

  3. POJ 1018 Communication System(树形DP)

    Description We have received an order from Pizoor Communications Inc. for a special communication sy ...

  4. poj 1018 Communication System

    点击打开链接 Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21007   Acc ...

  5. POJ 1018 Communication System (动态规划)

    We have received an order from Pizoor Communications Inc. for a special communication system. The sy ...

  6. POJ 1018 Communication System 贪心+枚举

    看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...

  7. POJ 1018 Communication System(DP)

    http://poj.org/problem?id=1018 题意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产,而每个厂家生产 ...

  8. POJ 1018 Communication System 题解

    本题一看似乎是递归回溯剪枝的方法.我一提交,结果超时. 然后又好像是使用DP,还可能我剪枝不够. 想了非常久,无奈忍不住偷看了下提示.发现方法真多.有贪心,DP,有高级剪枝的.还有三分法的.八仙过海各 ...

  9. poj 1018 Communication System_贪心

    题意:给你n个厂,每个厂有m个产品,产品有B(带宽),P(价格),现在要你求最大的 B/P 明显是枚举,当P大于一定值,B/P为零,可以用这个剪枝 #include <iostream> ...

随机推荐

  1. hdu2289Cup(神坑题,精度+二分,以半径二分不能过,以高度为二分就过了)

    Problem Description The WHU ACM Team has a big cup, with which every member drinks water. Now, we kn ...

  2. Linux/Mac OS 下 批量提交 新增文件到SVN 服务器

    命令行下操作svn没有使用界面形式的TortoiseSVN直观,但是不管怎样,命令行下操作svn还是有它的有点,如果你碰到一次需要svn add许多个文件怎么办?下面的命令可以帮助你解决这个问题 一次 ...

  3. Oauth1.0认证过程

    现今,已经有了Oauth2.0,写篇博客了解Oauth1.0的过程以及与2.0的区别. 在Oauth官网  关于1.0的介绍: 一.简介 OAuth authentication is the pro ...

  4. Java-Math

    java.lang.Math类提供的方法都是static的,“静态引入 ”使得不必每次在调用类方法时都在方法前写上类名:             import static java.lang.Mat ...

  5. ZOJ 3879 Capture the Flag 15年浙江省赛K题

    每年省赛必有的一道模拟题,描述都是非常的长,题目都是蛮好写的... sigh... 比赛的时候没有写出这道题目 :( 题意:首先输入4个数,n,q,p,c代表有n个队伍,q个服务器,每支队伍的初始分数 ...

  6. Strange Country II 暴力dfs

    这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索 //#pragma comment(linker, "/STACK:16777216") //for c++ Co ...

  7. web前端如何让网页布局稳定性和标准性?

    刚开始学css+div布局的同学们,都比较困惑和难写的就是兼容性的问题了,特别是ie6等低版本的浏览器,随意国内逐步慢慢消失取代,但是现阶段还是会有点考虑因素再里面.我们写的网页布局怎么样才是合理的, ...

  8. Python 2.7 学习笔记 异常处理

    如同别的开发语言,python也支持异常处理机制.本文介绍下它的基本语法. 一.异常的基本处理框架如下: try: 业务代码 except 异常类1: 异常处理代码 except 异常类2: 异常处理 ...

  9. Hdu 1158 Employment Planning(DP)

    Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1158 一道dp题,或许是我对dp的理解的还不够,看了题解才做出来,要加油了. 只能先上代码了 ...

  10. Android模拟器的文件目录介绍

    文件存放在 .avd文件夹下 .ini为对应的配置文件     打开.avd文件夹 *.lock文件夹保存的是模拟器的一下数据,当模拟器正常关闭时这些文件夹都会被自动删除. 当模拟器无法开启的时候可以 ...