POJ 1018 - Communication System (枚举)

本文介绍了一种针对通信系统的设备选型优化方法,旨在通过选择不同制造商提供的设备来最大化系统的整体性价比(B/P),其中B表示系统中各设备带宽的最小值,P表示所有设备的总价格。

Communication System
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 23066 Accepted: 8198

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

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest

[Submit]   [Go Back]   [Status]   [Discuss]



题意:

某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1、m2、m3、...、mn个厂家提供生产,而每个厂家生产的同种设备都会存在两个方面的差别:带宽bandwidths 和 价格prices。

现在每种设备都各需要1个,考虑到性价比问题,要求所挑选出来的n件设备,要使得B/P最大。

其中B为这n件设备的带宽的最小值,P为这n件设备的总价。


思路:

首先枚举最小的B值bx,对于每一个设备,找出符合bi>=bx并且p值最小的的厂家,然后每个设备的p值加起来就可以算出当前枚举的b值bx这种情况下的最优解

对于找出符合bi>=bx并且p最小的厂家,可以通过枚举的bx值的顺序做优化,bx又大到小,这样就可以直接维护每个设备当前p的最小值


C++


/*************************************************************************
	> Name: .test.cpp
	> Created Time: 2014年08月10日 星期日 15时09分40秒
 ************************************************************************/

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 100 + 20;

struct Node {
    int b, p;
    Node(int b=0, int p=0) : b(b), p(p) {}
};

vector<Node> G[maxn];
int S[maxn];
int Bs[maxn*maxn];

bool cmp(Node a, Node b) {
    if(a.b == b.b) return a.p < b.p;
    return a.b < b.b;
}

int main() {
    int T;

    scanf("%d", &T);
    while(T--) {
        int n;
        scanf("%d", &n);
        int bsn = 0;
        for(int i=0; i<n; i++) {
            int m;
            scanf("%d", &m);
            G[i].clear();
            S[i] = INF;
            for(int j=0; j<m; j++) {
                int b, p;
                scanf("%d%d", &b, &p);
                G[i].push_back(Node(b, p));
                Bs[bsn++] = b;
            }
            sort(G[i].begin(), G[i].end(), cmp);
        }
        sort(Bs, Bs+bsn);
        int num = 1;
        for(int i=1; i<bsn; i++) {
            if(Bs[i] != Bs[num-1]) Bs[num++] = Bs[i];
        }
        double ans = 0;
        for(int i=num-1; i>=0; i--) {
            int minb = Bs[i];
            double tansp = 0;
            int flag = 0;
            for(int j=0; j<n; j++) {
                vector<Node>::iterator it = G[j].end();
                --it;
                while(it->b >= minb) {
                    S[j] = min(S[j], it->p);
                    G[j].erase(it);
                    --it;
                }
                if(S[j] == INF) {
                    flag = 1;
                    break;
                }
                tansp += S[j];
            }
            if(flag) continue;
            double tans = minb / tansp;
            if(tans > ans) ans = tans;
        }
        printf("%.3lf\n", ans); // C++
    }

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值