DP第一题:poj1018---Communication System

Communication System
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 31193 Accepted: 11040
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
Sample Output

0.649

这道题有多种做法,暴力也可以过
枚举+贪心的方法,O(n ^ 4)
转载:https://blog.csdn.net/xiefubao/article/details/20737817
先将每种产品的供应情况排序,按带宽从小到大,然后记录一个后缀价格的最小值。枚举所有的带宽,当然是枚举给出的每种带宽,然后每种产品中求比此带宽大的最小价格;然后有一个剪枝就是当当前的比例已经小于更新的结果是就果断break减掉,不再算下去了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 110;
typedef struct Point{
    double width;
    double price;
}Point;
Point point[N][N];
int num[N];
double ans[N][N];
bool cmp(const Point &p,const Point &q)
{
    return p.width < q.width;
}
//O(n^4)也可以过,这样说深搜也可以过
//枚举+贪心,其实就是枚举每个带宽,将其作为要取的那个最小带宽,用贪心优化一下这样的暴力做法

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i = 0;i < n;++i)
        {
            scanf("%d",&num[i]);
            for(int j = 0;j < num[i];++j)
            {
                scanf("%lf %lf",&point[i][j].width,&point[i][j].price);
            }
            sort(point[i],point[i] + num[i],cmp);
            ans[i][num[i] - 1] = point[i][num[i] - 1].price;
            for(int j = num[i] - 2;j >= 0;--j)
            {
                ans[i][j] = min(point[i][j].price,ans[i][j + 1]);
            }
        }
//        for(int i = 0;i < n;++i)
//        {
//            printf("%d\n",num[i]);
//            for(int j = 0;j < num[i];++j)
//            {
//                printf("%.3lf %.3lf ",point[i][j].width,point[i][j].price);
//            }
//        }
        double ans1 = 0;
        for(int i = 0;i < n;++i)
        {
            for(int j = 0;j < num[i];++j)
            {
                double ptr = point[i][j].width;
                double sum = point[i][j].price;
                bool flag = false;
                for(int k = 0;k < n;++k)
                {
                    if(k == i) continue;
                    int p = -1;
                    for(int x = 0;x < num[k];++x){
                        if(point[k][x].width >= ptr){
                            p = x;
                            break;
                        }
                    }
                    if(p == -1){
                        flag = true;
                        break;
                    }
                    else{
                        sum += ans[k][p];
                    }
                    if(ptr / sum < ans1){
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    ans1 = max(ans1,ptr / sum);
                }
            }
        }
        printf("%.3f\n",ans1);
    }
    return 0;
}

DP解法:
dp[i][j]:表示前i-1个设备,带宽为j所需的最小价格
状态转移方程:dp[i][j] = min(dp[i][j],dp[i - 1][k] + p(价格));

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
int dp[110][1100];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i = 1;i <= n;++i)
        {
            for(int j = 0;j < 1100;++j)
            {
                dp[i][j] = inf;
            }
        }
        for(int i = 1;i <= n;++i)
        {
            int num;
            scanf("%d",&num);
            for(int j = 0;j < num;++j)
            {
                int b,p;
                scanf("%d %d",&b,&p);
                if(i == 1){
                    dp[i][b] = p;
                }
                else{
                    for(int k = 0;k < 1100;++k)
                    {
                        if(dp[i - 1][k] != inf){
                            if(k <= b){
                                dp[i][k] = min(dp[i][k],dp[i - 1][k] + p);
                            }
                            else{
                                dp[i][b] = min(dp[i][b],dp[i - 1][k] + p);
                            }
                        }
                    }
                }
            }
        }
        double ans = -1;
        for(int i = 0;i < 1100;++i)
        {
            if(dp[n][i] != inf){
                double x = (double)i / dp[n][i];
                ans = max(ans,x);
            }
        }
        printf("%.3f\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36386435/article/details/81272098