个人心得:百度推荐的简单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. loadrunder脚本篇——执行操作系统命令

    思路: 用loadrunner system()函数 函数原型: int system( const char *string ); 示例一:在指定目录下创建指定文件 Action() { char ...

  2. Ajax+Spring MVC实现跨域请求(JSONP)

    背景: AJAX向后台(springmvc)发送请求,报错:已阻止交叉源请求:同源策略不允许读取 http://127.0.0.1:8080/DevInfoWeb/getJsonp 上的远程资源.可 ...

  3. 前段时间说了AssetBundle打包,先设置AssetLabels,再执行打包,但是这样有个弊端就是所有设置了AssetLabels的资源都会打包,这次说说不设置AssetLabels,该如何打包AssetBundle

    BuildPipeline.BuildAssetBundles() 这个函数,有多个重载,一个不用AssetBundleBuild数组,一个需要,如果设置了AssetLabels,那么这时候是不需要的 ...

  4. 【转载】用Scikit-Learn构建K-近邻算法,分类MNIST数据集

    原帖地址:https://www.jiqizhixin.com/articles/2018-04-03-5 K 近邻算法,简称 K-NN.在如今深度学习盛行的时代,这个经典的机器学习算法经常被轻视.本 ...

  5. Struts2全局异常处理

    1.在struts.xml中配置全局异常处理 在Action中抛出异常,此异常可以是action自己抛的,也可以是Service抛出来的,都会跳转到全局异常中,只有在当前Action中配置域全局异常返 ...

  6. sqlserver 实时同步(发布订阅)

    配置发布订阅手册 不同版本须知:https://www.sqlmanager.net/en/articles/1548 向后兼容性:参考https://docs.microsoft.com/zh-cn ...

  7. Spark常用算子-value数据类型的算子

    package com.test; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; im ...

  8. 树的dfs序 && 系统栈 && c++ rope

    利用树的dfs序解决问题: 就是dfs的时候记录每个节点的进入时间和离开时间,这样一个完整的区间就是一颗完整的树,就转化成了区间维护的问题. 比如hdu3887 本质上是一个求子树和的问题 #incl ...

  9. JavaWeb -- Servlet+JSP+JavaBean(MVC)模式

    Servlet+JSP+JavaBean(MVC)模式适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp负责数据显示,javabean负责封装数据. Servlet+JSP ...

  10. RPM和yum相关

    写在前面: 在这里可以知道rpm和yum的基本用法,找到更新本地yum源.搭建yum源的方法以及yum很琐碎的东西,包括yum源的优先级.用yum来安装或卸载CentOS图形界面包以及保存yum下载的 ...