个人心得:百度推荐的简单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. 值得关注的10个Python语言学习博客

    大家好,还记得我当时学习python的时候,我一直努力地寻找关于python的博客,但我发现它们的数量很少.这也是我建立这个博客的原因,向大家分享我自己学到的新知识.今天我向大家推荐10个值得我们关注 ...

  2. ssh登陆virtualbox虚拟机

  3. SQL 根据IF判断,SET字段值

    当INVOICE_STATUS值为1时,赋值为2,否者赋值为原来的值 UPDATE T_INVOICE SET DOWNLOAD_COUNT = DOWNLOAD_COUNT + 1, INVOICE ...

  4. oracle长连接超时设置

    方法一.在sqlnet.ora中设置参数 如需要设置客户端空闲10分钟即被中断,则在sqlnet.ora的末尾添加SQLNET.EXPIRE_TIME=10注:sqlnet.ora文件的路径在$ORA ...

  5. Python mysql表数据和json格式的相互转换

    功能: 1.Python 脚本将mysql表数据转换成json格式 2.Python 脚本将json数据转成SQL插入数据库 表数据: SQL查询:SELECT id,NAME,LOCAL,mobil ...

  6. 20145239杜文超《网络对抗》- Web安全基础实践

    20145239杜文超<网络对抗>- Web安全基础实践 基础问题回答 (1)SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查 ...

  7. 20145240 《Java程序设计》第四周学习总结

    20145240 <Java程序设计>第四周学习总结 教材学习内容总结 6.1继承 6.1.1 继承共同行为 定义:继承基本上就是避免多个类间重复定义共同行为. 优点:1.提高了代码的复用 ...

  8. ubuntu ssh免密码登录

    目前很多服务(ceph,openstack等)都需要用到SSH使用ssh-key进行登录,而不能使用密码进行登录. 下面是配置步骤: 一.在SSH Client生成ssh key pair root@ ...

  9. 一键安装 lnmp/lamp/lanmp

    1.使用putty或类似的SSH工具登陆VPS或服务器 # screen -S lnmp 如果提示screen: command not found 命令不存在可以执行:yum install scr ...

  10. freemarker模板解析过程

    例如:一个freemarker表达式<body> ${hello} </body>,会被解析成三个部分,分别是<body>${hello}</body> ...