POJ 1018 Communication System DP

这道DP真是没想出来怎么做,不过看了别人的代码后发现也只能这么想了,只有B的状态是可以确定的,然后枚举B,

还可以再优化一下,但是数据量不大,就没这个必要了……

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 105;

vector<int> B[N], P[N]; //由于是不定长数组,所以用vector
int BMin, BMax;

void input( int& n ) { //输入数据,并且获取BMin, BMax的值.
    int m, b, p;
    scanf("%d", &n);
    for( int i = 0; i < n; ++i ) {
        B[i].clear(), P[i].clear();
        scanf("%d", &m);
        int temp = -1;
        while(m--) {
            scanf("%d%d", &b, &p);
            BMin = min(BMin, b);
            temp = max(temp, b);
            B[i].push_back(b), P[i].push_back(p);
        }
        BMax = min(BMax, temp);
    }
}

void slove( ) {
    BMin = 100000000, BMax = 100000000;
    int n; input(n); double ans = 0;
    for( int i = BMin; i <= BMax; ++i ) {
        double PSum = 0;
        for( int j = 0; j < n; ++j ) {
            int Size = B[j].size(), temp = 100000000;
            for( int k = 0; k < Size; ++k ) {
                if( B[j][k] >= i && P[j][k] < temp ) temp = P[j][k];
            }
            PSum += temp;
        }
        ans = max( ans, i / PSum );
    }
    printf("%.3lf
", ans);
}

int main() {
    int t;
    scanf("%d", &t); while(t--) slove();
    return 0;
}
原文地址:https://www.cnblogs.com/yaling/p/3452555.html