个人心得:百度推荐的简单DP题,自己做了下发现真得水,看了题解发现他们的思维真得比我好太多太多,

这是一段漫长的锻炼路呀。

关于这道题,我最开始用DP的思路,找子状态,发现自己根本就不会找DP状态数组建立,怎么找都是被后面的给打乱了,

看到了网上的DP【i】【j】,i表示前几个设备,宽带为j的最少花费,哇,真得厉害,突然发现动态规划并不需要做到问题很完美,

其实只要解决方案能够顺带把这个解决就好了。

他的这个转移方程就是,当i等于1时,输入的想,输入的x,y(x表示宽带长度,y表示价格)dp【i】【x】=y;

当i往后面递推时,如果此时i-1中k宽带存在的话,就跟此时的比较,如果此时的x小于K的话就可以直接放进去,大于的话就新的dp【i】【x】=min(dp【i】【x】,dp【i-1】【k】+y);

虽然此时的并不一定会是最优解,但是一步一步递推就把前面所有的情况都包含了进去,就可以一步一步得到前n个各个最小最大宽带数的最小价格;

动态规划的核心永远在状态的寻找,和转移方程的建立,看这里

  for(int i=;i<=n;i++){
int m;
scanf("%d",&m);
for(int j=;j<=m;j++){
int x,y;
scanf("%d%d",&x,&y);
if(i==){
dp[i][x]=y;
}
else
{
for(int k=;k<;k++)
if(dp[i-][k]!=inf)
{
if(k<=x)
dp[i][k]=min(dp[i][k],dp[i-][k]+y);
else
dp[i][x]=min(dp[i][x],dp[i-][k]+y);
} }
}

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<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[][];
const int inf=;
double mina(double x,double y){
return x<y?x:y;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<;j++)
dp[i][j]=inf;
for(int i=;i<=n;i++){
int m;
scanf("%d",&m);
for(int j=;j<=m;j++){
int x,y;
scanf("%d%d",&x,&y);
if(i==){
dp[i][x]=y;
}
else
{
for(int k=;k<;k++)
if(dp[i-][k]!=inf)
{
if(k<=x)
dp[i][k]=min(dp[i][k],dp[i-][k]+y);
else
dp[i][x]=min(dp[i][x],dp[i-][k]+y);
} }
}
}
double ans=;
for(int j=;j<;j++)
{
if(dp[n][j]!=inf)
{
double t=(double)j/dp[n][j];
if(t>ans)
ans=t;
}
}
printf("%.3f\n",ans);
}
return ;
}
												

Communication System(动态规划)的更多相关文章

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

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

  2. Communication System(dp)

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

  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. F - Communication System

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

  7. POJ 1018 Communication System(树形DP)

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

  8. poj 1018 Communication System (枚举)

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

  9. POJ1018 Communication System

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26738   Accepted: 9546 Description We ...

随机推荐

  1. iOS Healthkit 使用探索分析 🌛

    一 基本认知层面: HealthKit框架提供了一个结构,应用可以使用它来分享健康和健身数据.HealthKit管理从不同来源获得的数据,并根据用户的偏好设置,自动将不同来源的所有数据合并起来.应用还 ...

  2. solr、Lucene、IKAnalyzer这三者关系是怎样的?

    lucene 是开源搜索引擎 solr 是基于 lucene开发的搜索引擎 IK 是中文分词. lucene 不是一个搜索引擎,只是一个基础的文件索引工具包,或者叫“搜索引擎开发包”.不能单独作为程序 ...

  3. OpenGL帧缓存对象(FBO:Frame Buffer Object)

    http://blog.csdn.net/dreamcs/article/details/7691690 转http://blog.csdn.net/xiajun07061225/article/de ...

  4. 0625 Django 基础

    相关命令: 1 创建项目 django-admin startproject 项目名称 2 创建应用 python manage.py startapp app名称 3 启动项目 python man ...

  5. nodejs安装,配置环境,使用express建立一个新项目

    1.下载nodejs安装包 去nodejs官网下载最新版本就行,网址:http://nodejs.cn/download/,点击自己适用的系统,自动下载跟电脑操作系统位数符合的安装包, 下载下来安装包 ...

  6. class_alias--为一个类创建别名

    class_alias--为一个类创建别名 bool class_alias ( string $original , string $alias [, bool $autoload = TRUE ] ...

  7. time_t、pthread_t

    1.time_t实际上就是长整型long int;用来保存从1970年1月1日0时0分0秒到现在时刻的秒数!用time()这个函数获取! #ifndef __TIME_T #define __TIME ...

  8. Elasticsearch6.4.3安装

    Linux内存一定要1g以上! 首先要有jdk环境 要求1.8版本以上   elasticsearch是Java写的 将上传的 elasticSearch安装包解压 cd /home/elastics ...

  9. 键盘高级操作技巧【TLCL】

    Ctrl-a     移动光标到行首. Ctrl-e     移动光标到行尾. Ctrl-f     光标前移一个字符:和右箭头作用一样. Ctrl-b     光标后移一个字符:和左箭头作用一样. ...

  10. review23

    文件的创建与删除 当使用File类创建一个文件对象后,例如 File file = new File("C:\\myletter", "letter.txt") ...