您的位置:首页 > 其它

poj 1018 Communication System(DP)

2016-03-23 13:26 507 查看
题意:

一个通信系统需要n个设备,每个设备有两个属性带宽B和价格P,第i个设备有mi个厂家生产,从中挑选n个设备,问n个设备中最小的带宽/总价格(B/P)的最大值是多少;

思路:

dp[i][j]表示前i个设备中带宽为j的最小费用(相同带宽费用越小,B/P越大)

dp[i][j]=min(dp[i][j],dp[i-1][j]+p);

#include<iostream>
#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>
using namespace std;

const int INF=0x3f3f3f3f;
const int maxn=1005;
int t,n,m;
int dp[105][maxn];//dp[i][j]表示前i个设备带宽为j的最小费用

int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
#endif
scanf("%d",&t);
while(t--){
scanf("%d",&n);
memset(dp,INF,sizeof(dp));
for(int i=1;i<=n;i++){
int m,b,p;
scanf("%d",&m);
for(int j=0;j<m;j++){
scanf("%d%d",&b,&p);
if(i==1){
dp[i][b]=p;
}
else{
for(int k=0;k<maxn;k++){
if(dp[i-1][k]!=INF){//须保证带宽为k的设备存在
if(k>b){
dp[i][b]=min(dp[i][b],dp[i-1][k]+p);
}
else{
dp[i][k]=min(dp[i][k],dp[i-1][k]+p);
}
}
}
}
}
}
double res=0;
for(int i=0;i<maxn;i++){
if(dp
[i]==INF) continue;
double tmp=i*1.0/dp
[i];
if(tmp>res)
res=tmp;
}
printf("%.3f\n",res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: