POJ 1018 Communication System(贪心)
Description
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
Output
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
#include <iostream>
#include <vector> using namespace std; struct goods
{
int p, b;
};
vector<goods>a[];
const int MAXN = ; void Clear() //用来清空信息的 很重要 没有这个会错
{
for (int i = ; i <= ; i++)
a[i].clear();
} int main()
{
int t, num, n;
double ans;
goods a1, a2;
cin >> t;
while (t--)
{
Clear();
ans = -;
cin >> num; //器件总数
for (int i = ; i < num; i++)
{
cin >> n;
for (int j = ; j < n; j++)
cin >> a1.b >> a1.p, a[i].push_back(a1);
}
for(int i = ; i < num; i++) //枚举第i个器件
{
for (int j = ; j < a[i].size(); j++) //枚举第i种器件的对应器件 确定第i种器件
{
a1 = a[i][j]; //选中第i种器件的第j个 而且假定这个器件的B是最小的
double B = a1.b;
double P = a1.p;
int pp;
for (int k = ; k < num; k++) //枚举n-1种器件
{
if (k == i) continue; //第i种器件已经选择过了 避免重复选择
pp = MAXN;
for (int h = ; h < a[k].size(); h++)
{
a2 = a[k][h];
if (a2.b < B) continue; //假定B是最小的 不能选择更小的
if (pp > a2.p) pp = a2.p; //为了使B/P尽可能大 P就要尽可能小
}
if (pp == MAXN) break; //找不到合适的器件 那么就表示选错了 重新选择
P += pp;
}
if (pp == MAXN) break;
if (ans < (B / P)) ans = (B / P);
}
}
printf("%.3f\n", ans);
}
return ;
}
POJ 1018 Communication System(贪心)的更多相关文章
- POJ 1018 Communication System 贪心+枚举
看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...
- poj 1018 Communication System 枚举 VS 贪心
Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21631 Accepted: ...
- POJ 1018 Communication System(树形DP)
Description We have received an order from Pizoor Communications Inc. for a special communication sy ...
- poj 1018 Communication System
点击打开链接 Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21007 Acc ...
- POJ 1018 Communication System (动态规划)
We have received an order from Pizoor Communications Inc. for a special communication system. The sy ...
- poj 1018 Communication System (枚举)
Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22380 Accepted: ...
- POJ 1018 Communication System(DP)
http://poj.org/problem?id=1018 题意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产,而每个厂家生产 ...
- POJ 1018 Communication System 题解
本题一看似乎是递归回溯剪枝的方法.我一提交,结果超时. 然后又好像是使用DP,还可能我剪枝不够. 想了非常久,无奈忍不住偷看了下提示.发现方法真多.有贪心,DP,有高级剪枝的.还有三分法的.八仙过海各 ...
- poj 1018 Communication System_贪心
题意:给你n个厂,每个厂有m个产品,产品有B(带宽),P(价格),现在要你求最大的 B/P 明显是枚举,当P大于一定值,B/P为零,可以用这个剪枝 #include <iostream> ...
随机推荐
- sql子查询
一.子查询入门: 1.单值子查询: 单值子查询的唯一限制:子查询的返回值必须只有一行记录,而且只能有一列(又被称为标量子查询). 可以使用在select语句的列表.表达式中,以及where语句中等. ...
- Android-第一天
1.google 2.application->application framework->libraries(调用关系) 3.strings.xml 是全局字符串的配置文件 4.ADT ...
- Node.js进阶:5分钟入门非对称加密方法
前言 刚回答了SegmentFault上一个兄弟提的问题<非对称解密出错>.这个属于Node.js在安全上的应用,遇到同样问题的人应该不少,基于回答的问题,这里简单总结下. 非对称加密的理 ...
- tone() 和 IRremote 冲突的解决办法
tone()函数冲突 http://www.geek-workshop.com/thread-4037-1-1.html 可以自制函数newtone() void newtone(byte toneP ...
- iOS学习——获取当前最顶层的ViewController
在iOS开发过程中,我们经常性会需要获取当前页面的ViewController,然后利用ViewController进行一些操作,例如在最顶层的ViewController上展示一个UIAlertCo ...
- JDBC(四)
1 Apache DBUtils框架 1.1 DBUtils简介 commons-dbutils是Apache组织提供的一个开源JDBC工具类库,它是对JDBC的简单封装,学习成本非常低,并且使用db ...
- js设置滚动条位置
JS控制滚动条的位置: window.scrollTo(x,y); 竖向滚动条置顶(window.scrollTo(0,0); 竖向滚动条置底 window.scrollTo(0,document.b ...
- SQL语句-delete语句
delete语句 delete语句用于删除表中已经存在的整行数据 Tbl_name关键词代表删除数据的目标表 Where子句代表被删除数据的满足条件,如果没有where子句则代 表所有表数据都删除 O ...
- ehcache模糊批量移除缓存
目录 前言 实现 总结 前言 众所周知,encache是现在最流行的java开源缓存框架,配置简单,结构清晰,功能强大.通过注解@Cacheable可以快速添加方法结果到缓存.通过@CacheEvic ...
- Centos更改镜像源
阿里云Linux安装镜像源: 1.备份原镜像文件以便于恢复. [root@localhost ~]#mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repo ...