poj 1018 Communication System(DP)

来源:互联网 发布:昆明行知中学怎么样 编辑:程序博客网 时间:2024/05/02 02:40

题意:

一个通信系统需要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_JUDGEfreopen("test.in","r",stdin);freopen("test.out","w",stdout);#endifscanf("%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[n][i]==INF) continue;double tmp=i*1.0/dp[n][i];if(tmp>res)res=tmp;}printf("%.3f\n",res);}return 0;}


0 0
原创粉丝点击