Vijos P1198 - 最佳课题选择

传送门

Vijos P1198 - 最佳课题选择

思路

$dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + Cal(i, k))$

dp[i][j]表示前i个主题写j篇论文的最少时间。

初始化要注意。
刚开始全部初始化为INF,WA了。有论文数是0的数据。

代码

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 200 + 5;
const int INF = 0x3f3f3f3f;
 
struct ARTC
{
    LL a, b;
}artc[MAXN];
 
LL dp[30][MAXN];
 
LL Cal(LL cur, LL num)
{
    LL t = 1;
    for (int i = 0; i < artc[cur].b; i++)
        t *= num;
    return t * artc[cur].a;
}
 
int main()
{
    //freopen("input.txt", "r", stdin);
    LL ntopc, npp, i, j;
    scanf("%lld%lld", &npp, &ntopc);
    for (i = 1; i <= ntopc; i++)
        scanf("%lld%lld", &artc[i].a, &artc[i].b);
    for (i = 1; i <= ntopc; i++)
        for (j = 1; j <= npp; j++)
            dp[i][j] = INF;
    for (i = 1; i <= npp; i++)
        dp[1][i] = Cal(1, i);
    for (i = 2; i <= ntopc; i++)
        for (j = 1; j <= npp; j++)
        {
            dp[i][j] = dp[i - 1][j];
            for (LL k = 1; k <= j; k++)
                dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + Cal(i, k));
        }
    printf("%lld\n", dp[ntopc][npp]);
    return 0;
}

Powered by Jekyll and Theme by solid