• 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

    Sample Output
    0.649

    分析:
    说实话我想到了这道题:
    Tyvj上的某题

    那道题是有两个界限,但是优先考虑最大价值

    然而这道题需要我们两个因素一起考虑(没有优先级了)

    我一开始是这样simple的考虑的
    f[i][j] 表示第i个设备由第j个制造商制造
    每次比较一下x1,x2再进行转移

    double x1=(double)f[i][j][0]/(double)f[i][j][1];
    double x2=(double)min(f[i-1][k][0],b[i][j])/(double)(f[i-1][k][1]+p[i][j]);

    但是交上去就是WA
    在对拍的时候发现是P=0时会停止运行(虽然不知道为什么会=0)
    改了之后继续对拍就没问题了
    然而交上去还是WA

    正解:
    f[i][j]表示第i行,目前最小b的值为j,sigma p的最小值
    ans=max(ans,i/f[n][i])

    tip

    这又能说明一件事
    样例都是骗人的

    这给我们一个启示:
    如果有两个变量,但是变化范围不大,那我们考虑

    枚举其中一个变量

    还有一种贪心做法:
    每次枚举b最小值,将所有b大于最小值的组加入队列,然后每行中选取p最小的,计算答案。

    语言:C++

    G++就一直WA

    这里写代码片
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<cmath>
    
    using namespace std;
    
    const double eps=1e-10;
    double ans;
    int n,num[102],b[102][102],p[102][102],f[102][1002];
    
    int dcmp(double x)
    {
        if (fabs(x)<eps) return 0;
        else if (x>0) return 1;
        else return -1;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while (T--)
        {
            scanf("%d",&n);
            ans=0.0;
            for (int i=1;i<=n;i++)
            {
                scanf("%d",&num[i]);  //数量
                for (int j=1;j<=num[i];j++)
                    scanf("%d%d",&b[i][j],&p[i][j]);   //第i个产品,第j个制造商制造  
            }
            int i,j,k;
            memset(f,127/3,sizeof(f));
            for (i=1;i<=num[1];i++) f[1][b[1][i]]=p[1][i];  //第i个产品,min b=j,最小p 
            for (i=2;i<=n;i++)
                for (j=1;j<=num[i];j++)
                    for (k=0;k<1000;k++)
                    {
                        if (k>=b[i][j]) f[i][b[i][j]]=min(f[i][b[i][j]],f[i-1][k]+p[i][j]);
                        else f[i][k]=min(f[i][k],f[i-1][k]+p[i][j]);
                    }
            for (i=0;i<1000;i++)
            {
                double x1;
                x1=(double)i/(double)f[n][i];
                if (dcmp(ans-x1)<0) ans=x1; 
            }
            printf("%0.3lf
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    如何将Superset嵌入后台系统之实践
    如何将Superset嵌入后台系统之实践
    SQL server添加链接服务器脚本
    面试题:JQuery有几种选择器?
    数据库增加索引
    Mybatis与Ibatis比较
    jQuery 选择器
    Form 和 Input 对象
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673191.html
Copyright © 2020-2023  润新知