POJ 1018 Communication System(DP+离散化)

来源:互联网 发布:魅族官网商城下载软件 编辑:程序博客网 时间:2024/05/17 23:26

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

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

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


思路:最多就1W个带宽,可以对带宽进行离散化,然后DP,dp[i][j]表示选到第i个,带宽为j的P总和的最小值,然后状态转移即可


代码:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 10005;const int INF = 0x3f3f3f3f;int t, n, dp[2][N], hash[N], hn;struct Man {        int m, b[105], p[105];    void read() {scanf("%d", &m);for (int i = 0; i < m; i++) {    scanf("%d%d", &b[i], &p[i]);    hash[hn++] = b[i];}    }} man[105];int find(int x) {    return lower_bound(hash, hash + hn, x) - hash;}int main() {    scanf("%d", &t);    while (t--) {hn = 0;scanf("%d", &n);for (int i = 1; i <= n; i++) man[i].read();sort(hash, hash + hn);hn = unique(hash, hash + hn) - hash;memset(dp[0], INF, sizeof(dp[0]));for (int j = 0; j < man[1].m; j++)    dp[0][find(man[1].b[j])] = man[1].p[j];int now = 0, pre = 1;for (int i = 2; i <= n; i++) {    swap(now, pre);    memset(dp[now], INF, sizeof(dp[now]));    for (int j = 0; j < man[i].m; j++) {for (int k = 0; k < hn; k++) {    int tmp = min(find(man[i].b[j]), k);    dp[now][tmp] = min(dp[now][tmp], dp[pre][k] + man[i].p[j]);}    }}double ans = 0;for (int i = 0; i < hn; i++)    ans = max(ans, hash[i] * 1.0 / dp[now][i]);printf("%.3f\n", ans);    }    return 0;}


0 0
原创粉丝点击