天天看點

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;
}