Communication System(动态规划)
个人心得:百度推荐的简单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
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
#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(动态规划)的更多相关文章
- POJ 1018 Communication System (动态规划)
We have received an order from Pizoor Communications Inc. for a special communication system. The sy ...
- Communication System(dp)
Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25006 Accepted: 8925 ...
- poj 1018 Communication System
点击打开链接 Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21007 Acc ...
- poj 1018 Communication System 枚举 VS 贪心
Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21631 Accepted: ...
- POJ 1018 Communication System(贪心)
Description We have received an order from Pizoor Communications Inc. for a special communication sy ...
- F - Communication System
We have received an order from Pizoor Communications Inc. for a special communication system. The sy ...
- 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: 22380 Accepted: ...
- POJ1018 Communication System
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26738 Accepted: 9546 Description We ...
随机推荐
- Linux服务器iops性能测试-fio
FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, null, ...
- Oracle索引(1)概述与创建索引
索引是为了提高数据检索效率而创建的一种独立于表的存储结构,由Oracle系统自动进行维护. 索引的概述 索引是一种可选的与表或簇相关的数据库对象,能够为数据的查询提供快捷的存储路径,减少 ...
- FTP下载
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import ja ...
- [SCOI2003]蜘蛛难题
题目 对于当年来说似乎是神题?? 做法 对于联通注水来说,我们考虑把所有能平分到水的桶同时加高度,然后暴力判断 My complete code copy来的代码 #include <cstdi ...
- 适配iOS9问题汇总
iOS 9适配过程中出现的问题,收集的链接资料供大家学习分享. http://wiki.mob.com/ios9-对sharesdk的影响(适配ios-9必读)/ http://www.cocoach ...
- 发现一个小坑的地方,unity的协程,想要停止,必须以字符串启动
今天想要停止一个协成,发现调用 StopCoroutine(ShowDebug()); 竟然不管用,后来看了文档才知道,原来想要停止协成,必须用字符启动协程 StartCoroutine(" ...
- 一步一步粗谈linux文件系统(三)----超级块(superblock)【转】
本文转载自:https://blog.csdn.net/fenglifeng1987/article/details/8302921 超级块是来描述整个文件系统信息的,可以说是一个全局的数据结构,可以 ...
- TCP的三个接收队列
之前对于TCP接收过程中的三个队列的关系之前没搞清楚. 这几天,在同事邱的帮助下,终于把关系理清了,故特此做个笔记. 一.在软中断中加入数据包 tcp_v4_rcv()函数是tcp层收包的入口. in ...
- makefile下$(wildcard $^),$^,$@,$?,$<,$(@D),$(@F) 含义
makefile下$(wildcard $^),$^,$@,$?,$<,$(@D),$(@F)代表的不同含义 $(filter-out $(PHONY) $(wildcard $^),$^) 常 ...
- poj 2478 Farey Sequence 欧拉函数前缀和
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Description The Farey Sequence Fn for ...