Communication System POJ1018(动态规划)

Communication SystemPOJ - 1018

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
分析:
和一般的背包问题还是有一定区别的,固定一个B的下限j,设dp[i][j]表示B下限为j的时候,前i种设备的最小价值;算出所有的dp[i][j],求解对应的j/dp[i][j],比较得出最大值即可。

其中dp[i][j]=min{dp[i][j],dp[i-1][j]+p[i][k]}(当j<=b[i][k]时)

p[i][k],b[i][k]是第i个设备的第k家企业的对应值

需要注意的是,题目并没有给出j的范围(即b的范围),我们可以将所有的输入b都存到一个vector里面,然后一个一个取出来试

当然,如果大概能判断j的可能范围500左右的话,下面代码里面的x直接从1~500枚举也可以的

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<stack>   
#include<set>  
#include<bitset>  
#include<list>
#include<iomanip>

#define UP(i,x,y) for(int i=x;i<=y;i++)  
#define DOWN(i,x,y) for(int i=x;i>=y;i--)  
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a) 
#define ll long long  
#define INF 0x3f3f3f3f  
#define EXP 1e-10  
#define lowbit(x) (x&-x)
 
using namespace std;
vector<int> b[110],p[110],pp;
int dp[110][500];
int main(){
	int t;cin>>t;
	while(t--){
		MEM(dp,0x3f);
		for(int i=0;i<500;i++){
			dp[0][i]=0;
		}
		int n;double ans=-INF; 
		cin>>n;
		for(int i=1;i<=n;i++){
			int m;cin>>m;
			for(int j=1;j<=m;j++){
				int B,P;
				cin>>B>>P;
				b[i].push_back(B);
				p[i].push_back(P);
				pp.push_back(P); 
			}
		}
		sort(pp.begin(),pp.end());
		for(int i=0;i<pp.size();i++){
			int x=pp[i];
			int ok,flag=INF;
			for(int j=1;j<=n;j++){
				ok=0;
				for(int k=0;k<b[j].size();k++){
					if(b[j][k]>=x&&dp[j][x]>dp[j-1][x]+p[j][k]){
						ok=1;
						dp[j][x]=dp[j-1][x]+p[j][k];
						flag=min(flag,b[j][k]);
					}
				}
				if(ok==0){
					break;
				}
			}
			if(ok==0)continue;
			double tans=double(flag)/dp[n][x];
			if(tans>ans)ans=tans;
		}
		cout<<fixed<<setprecision(3)<<ans<<endl;
		for(int i=1;i<=n;i++){
			b[i].clear();p[i].clear();
		}
		pp.clear();
	} 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41333528/article/details/80482566