poj 1018 Communication System
来源:互联网 发布:清华 软件硕士 编辑:程序博客网 时间:2024/06/05 11:42
首先做这个题是极其难受的,第一次去做dp的题,万事开头难,虽然以前也接触过dp,但是都极力躲避着用其他算法解决,现在专门练这部分,倒觉得,你拍什么,什么就会比想象中难很多,没办法,一点一点练吧!
题意是说有n个设备,对于每个设备,有m个制造商,每个制造商对应不同的带宽(B)和价格(P)求出选出的n个设备,使得总的B/p 最大。题意弄了半天才搞懂,开始就写错了,后来也是看了别人的代码,才发觉是01背包问题,狗血的是c++ 47Ms秒过,g++wa!
code:
#include<iostream>#include<cstdio>#include<cstring>#include <algorithm>using namespace std;int dp[101][1010];int b[101],p[1010];int main (){ int t,n,m; scanf("%d",&t); while(t--) { int maxb; memset(dp,-1,sizeof(dp)); scanf("%d",&n); maxb = 0; for(int i = 1;i <= n;++i) { scanf("%d",&m); for(int j = 1;j <= m;++j) { scanf("%d%d",&b[j],&p[j]); maxb = max(maxb,b[j]); } if(i == 1) { for(int j = 1;j <= m;++j) dp[1][b[j]] = p[j]; continue; } for(int j = 0;j <= maxb;++j) { if(dp[i-1][j] != -1) { for(int k = 1;k <= m;++k) { int tb = min(j,b[k]); if(dp[i][tb] == -1) dp[i][tb] = dp[i-1][j] + p[k]; else dp[i][tb] = min(dp[i][tb],dp[i-1][j]+ p[k]); } } } } double ans = 0; for(int j = 1;j <= maxb;++j) { if(dp[n][j] != -1) ans = max(ans,((double)j/dp[n][j])); } printf("%.3lf\n",ans); } return 0;}
0 0
- poj 1018 Communication System
- POJ 1018 Communication System
- poj 1018 Communication System
- POJ 1018 Communication System
- POJ 1018 Communication System
- POJ 1018 Communication System
- poj 1018 Communication System
- POJ 1018 Communication System
- poj 1018 Communication System
- poj 1018 Communication System
- POJ 1018 Communication System
- poj 1018 Communication System
- poj 1018 Communication System
- poj-1018 Communication System
- POJ-1018 Communication System
- [POJ][1018]Communication System
- poj 1018Communication System
- poj 1018:Communication System
- 忘记手势密码的解决办法
- 云盘搜索找资料
- 设计师必备的html工具
- C语言整理-6
- dp题目分类
- poj 1018 Communication System
- 大娱乐家
- HDU——2014 青年歌手大奖赛_评委会打分
- HDU中一些DP的题目分类
- 背包九讲
- C++中几种将整数转换成二进制输出的方法
- 快速pow和sqrt的小技巧 hdu4282
- pow(x,y)函数
- ZOJ 2060----Fibonacci Again