POJ 1018 Communication System(DP+离散化)

某公司要建立一套通信系统,该通信系统需要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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值