本题一看似乎是递归回溯剪枝的方法。我一提交,结果超时。

然后又好像是使用DP,还可能我剪枝不够。

想了非常久,无奈忍不住偷看了下提示。发现方法真多。有贪心,DP,有高级剪枝的。还有三分法的。八仙过海各显神通啊。

坏习惯了,没思考够深入就偷看提示了。

幸好及时回头,还不须要看别人的代码了。自己做出来之后,有空看看多种解法的代码也好。

然后我想出自己的思路了,使用贪心,剪枝,DP综合优化下,呵呵。最后程序有点复杂。优化到了16ms,运气好点,或者vector换成原始数组的话,应该能够0MS了。

整体思路就是:

1 利用STL 的set容器记录有多少不同的B值

2 依据不同的B值,用表tbl记录该B值下的最优解

最后比較全部B值下的最优解。得出终于最优解。

以下是优化过的程序,用了不少技巧,加了凝视,希望提高參考价值吧。

#include <stdio.h>
#include <float.h>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <set>
using namespace std; const int MAX_N = 101;
int N, M; struct BP
{
int B, P;
bool operator<(const BP &b) const
{
return B < b.B;
}
}; BP arr[MAX_N][MAX_N]; float DP(set<int> &bset)
{
for (int i = 0; i < N; i++)
{
for (int j = arr[i][0].B-1; j > 0 ; j--)
{
arr[i][j].P = min(arr[i][j].P, arr[i][j+1].P);
}//计算结果为当前大于某个B的最小P值,优化以下填表
} vector<int> bvec(bset.begin(), bset.end());
int M = (int)bvec.size(); //总共同拥有多少个不同的B值
vector<vector<int> > tbl(N, vector<int>(M));//记录当前B下的最优P值
vector<int> idx(N, 1); //arr行的当前下标 for (int j = 0; j < M; j++)
{
for (int i = 0; i < N; i++)
{
for ( ; idx[i] <= arr[i][0].B; idx[i]++)
{
if (arr[i][idx[i]].B >= bvec[j])
{
tbl[i][j] = arr[i][idx[i]].P;
break;
}
}
if (idx[i] > arr[i][0].B)//某行无法选出比B更大的值了
{
tbl[0][j] = -1;//做好标志,剪枝
goto out;
}
}
}
out:;
float ans = 0.0f;
for (int j = 0; j < M && tbl[0][j] != -1; j++)
{
int totalP = 0;
for (int i = 0; i < N; i++)
{
totalP += tbl[i][j];
}
ans = max(ans, float(bvec[j])/float(totalP));
}
return ans;
} int main()
{
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d", &N);
set<int> bset;
for (int i = 0; i < N; i++)
{
scanf("%d", &arr[i][0].B); //记录当前维长度
for (int j = 1; j <= arr[i][0].B; j++)
{
scanf("%d %d", &arr[i][j].B, &arr[i][j].P);
bset.insert(arr[i][j].B);//记录有多少个不同的B值
}
sort(arr[i]+1, arr[i]+arr[i][0].B+1);//每维按B值由小到大排序
}
printf("%.3f\n", DP(bset));
}
return 0;
}

POJ 1018 Communication System 题解的更多相关文章

  1. POJ 1018 Communication System(树形DP)

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

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

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

  3. poj 1018 Communication System

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

  4. poj 1018 Communication System 枚举 VS 贪心

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

  5. POJ 1018 Communication System(贪心)

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

  6. poj 1018 Communication System (枚举)

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

  7. POJ 1018 Communication System(DP)

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

  8. POJ 1018 Communication System 贪心+枚举

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

  9. poj 1018 Communication System_贪心

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

随机推荐

  1. luogu2590 [ZJOI2008]树的统计

    树剖裸题 #include <iostream> #include <cstdio> using namespace std; int n, uu, vv, hea[30005 ...

  2. STW Family

    序言 一次作业致使我们成为了一个团体,相聚即是缘分,让我们一起为STW绘制一幅完美的画卷,交一份满意的答卷. 这不是一个人的王者,是团队的荣耀. Team成员 队长:王筱哲 201631062220 ...

  3. acmer之ubuntu下安装Eclipse

    ubuntu是acmer常用的系统,配置起CB还是比较简单的三行命令就OK了 //Current stable version of Code::Blocks IDE (16.01) //To ins ...

  4. Spring c3p0连接池通过Hibernate配置

    首先进行Hibernate配置,详见http://www.cnblogs.com/claricre/p/6509931.html 然后调用这三个包. 配置hibernate.cfg.xml文件: &l ...

  5. mysql经验总结

    1. 连接mysql遇到 Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2),mysql没有正常启动 $ ...

  6. redis学习(一)redis简介

    REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统,是一种NoSql数据库.Redis是一个开源的使用ANS ...

  7. mybatis学习(十一)——springmvc++spring+mybatis整合

    做任何一个项目都以一个需求,这里先定义一下需求:利用三大框架查询酒店列表. 一.搭建开发环境 1.创建一个web项目 我这里用的是 jdk1.8+tomact7.0 2.创建hotel表 CREATE ...

  8. 【SCOI2003】【BZOJ1092】蜘蛛难题

    有一堆管道,还有一个蜘蛛Willy,如下图所示.所有管道的是上端开口,下端封底,直径都是1cm,连接两个管道的连接容量无限,但体积可以忽略不计. 在第一个管道上方有一个水源,从中有水不断往下流,速度为 ...

  9. eclipse testng插件安装

    1.安装Testng 在Eclipse中,点击Help→Install new software→点击Add,在location中输入http://beust.com/eclipse ,选择TestN ...

  10. POJ3671 Dining Cows

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8126   Accepted: 3441 Description The c ...