51Nod-1398-等公交

ACM模版

描述

51Nod-1398-等公交_第1张图片

题解

暴力 dp,时间复杂度为 O(Ns) ,代码十分容易理解,不再赘述了……

代码

#include 
#include 
#include 

#define clr(a, b) memset(a, b, sizeof(a))

using namespace std;

const int MAXN = 100;
const int MAXS = 2e5 + 5;

int N, s;
int tim[MAXN];
double pro[MAXN];
double dp[MAXS];

int main ()
{
    int T;
    scanf("%d", &T);

    while (T--)
    {
        clr(dp, 0);
        dp[0] = 1;

        scanf("%d%d", &N, &s);
        for (int i = 0; i < N; i++)
        {
            scanf("%d%lf", tim + i, pro + i);
            pro[i] /= MAXN;
        }

        for (int i = 0; i < s; i++)
        {
            for (int j = 0; j < N; j++)
            {
                dp[i + tim[j]] += dp[i] * pro[j];
            }
        }

        double ans = 0;
        for (int i = s + 1; i < MAXS; i++)
        {
            ans += (i - s) * dp[i];
        }

        printf("%.4lf\n", ans);
    }

    return 0;
}

你可能感兴趣的:(动态规划,51Nod-题解集锦)