POJ 1018 Communication System (贪心 DP)

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

代码

贪心

用结构体储存每一组设备
对每一组设备p升序排序,枚举k的值,每一组设备取b大于等于k且p最小的…
这里k的值是有上限的,上限为每一组设备b的最大值的最小值,可以进行优化

#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<vector>
#include<stdio.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
    int b,p;
};
node w[105][105];
int v[105];
bool cmp(node a,node b)
{
    return a.p<b.p;
}
int main()
{
    int t,n,b,p;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int mi=inf;//每组设备最大值的最小值
        for(int i=0; i<n; ++i)
        {
            int ma=0;
            scanf("%d",&v[i]);
            for(int j=0; j<v[i]; ++j)
            {
                scanf("%d %d",&w[i][j].b,&w[i][j].p);
                if(ma<w[i][j].b)
                    ma=w[i][j].b;
            }
            if(mi>ma)
                mi=ma;//取得k的上限
        }
        for(int i=0; i<n; ++i)
            sort(w[i],w[i]+v[i],cmp);
            double ans=0;
        for(int k=0; k<=mi; ++k)
        {
            int mi,all=0;
            for(int i=0; i<n; ++i)
            {
                for(int j=0; j<v[i]; ++j)
                    if(w[i][j].b>=k)
                    {
                        mi=w[i][j].p;
                        break;
                    }
                all+=mi;
            }
            double ne=k*1.0/all;
            if(ans<ne)
                ans=ne;
        }
        printf("%.3f\n",ans);
    }
}

dp

#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<vector>
#include<stdio.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int dp[105][1005],v[105];
int main()
{
    int t,n,b,p;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,inf,sizeof(dp));
        scanf("%d",&n);
        for(int i=1; i<=n; ++i)
        {
            scanf("%d",&v[i]);
            for(int j=1; j<=v[i]; ++j)
            {
                scanf("%d %d",&b,&p);
                if(i==1)
                    dp[1][b]=min(dp[1][b],p);
                else
                {
                    for(int k=0; k<1005; ++k)
                    {
                        if(dp[i-1][k]==inf)
                            continue;
                        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 ans=0;
        for(int i=0;i<1005;++i)
        {
            if(dp[n][i]==inf)
                continue;
            double ne=i*1.0/dp[n][i];
            if(ne>ans)
                ans=ne;
        }
        printf("%.3f\n",ans);
    }
}
发布了145 篇原创文章 · 获赞 30 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43984169/article/details/95649360