# POJ1018总结

## Communication System

### Description

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```

`0.649`

### 我的代码

``````#include<stdio.h>
#include<string.h>
const int inf=0x3f3f3f3f;
struct device
//记录每一个i个设备所有供应商给出的带宽和价格
{
int num;
int b[101];
int p[101];
device()
{
num=0;
}
}dev[101];
int n;
int dp[101][1000];
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for (int i=0;i<n;i++)
{
scanf("%d",&dev[i].num);
for (int j=0;j<dev[i].num;j++)
{
scanf("%d%d",&dev[i].b[j],&dev[i].p[j]);
}
}
for (int i=0;i<1000;i++)
{
int min=inf;
for (int j=0;j<dev[0].num;j++)
{
if (dev[0].b[j]>=i)
{
if (dev[0].p[j]<min)
{
min=dev[0].p[j];
}
}
}
dp[0][i]=min;
}
for (int i=1;i<n;i++)
{
for (int b=0;b<1000;b++)
{
int min=inf;
for (int j=0;j<dev[i].num;j++)
{
if (dev[i].b[j]>=b)
{
if (dev[i].p[j]<min)
{
min=dev[i].p[j];
}
}
}
dp[i][b]=dp[i-1][b]+min;
}
}
double ans=0;
for (int b=0;b<1000;b++)
{
if (ans<(1.0*b/dp[n-1][b]))	ans=(1.0*b/dp[n-1][b]);
}
printf("%.3f\n",ans);
}
return 0;
}``````

## 另一种dp思路

dp[][]数组含义相同，但是处理方式不一样。方程dp[i][k] = min(dp[i][k],dp[i-1][j]+price)。如果bandwidth <= j，那么k = bandwidth；反之，k = j.直接看方程可能不是很明白，可以结合代码来看。

``````for(int k=0;k<M;k++){    //枚举带宽
if(band >= k)	dp[i][k] = min(dp[i][k],dp[i-1][k]+price);   //寻找最小的带宽
else	dp[i][band] = min(dp[i][band],dp[i-1][k]+price);
}``````

``````#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int const inf = 0x7f7f7f7f;
int const N = 100;
int const M = 1200;
int n,m,band,price,dp[N+10][M+10];
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=0;i<=n;i++){
for(int j=0;j<M;j++)
if(i == 0)	dp[i][j] = 0;
else dp[i][j] = inf;
}
for(int i=1;i<=n;i++){   //总共有n个设备，每个设备挑一个，并且使他们的B/P最小。
scanf("%d",&m);
for(int j=1;j<=m;j++){
scanf("%d%d",&band,&price);
for(int k=0;k<M;k++){    //枚举带宽
if(band >= k)	dp[i][k] = min(dp[i][k],dp[i-1][k]+price);   //寻找最小的带宽
else	dp[i][band] = min(dp[i][band],dp[i-1][k]+price);
}
}
}
double ans = 0;
for(int i=1;i<M;i++)
ans = max(ans,i*1.0/dp[n][i]);
printf("%.3f\n",ans);
}
return 0;
}``````