hdu1074(状压DP)

这题本来很简单,代码被我写搓了,字母翘错了一个一直不出结果。

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<map>
#include<stack>
using namespace std; typedef long long lld;
#define oo 0x3f3f3f3f
#define maxn 1<<16
int mark[maxn]; struct DP { int min_re_s; int time; int pre; int key; }dp[maxn]; struct Class { int dealine; int time; char name[102]; }a[16]; void OutPutAns(int st) { if (dp[st].pre == -1) return;
    OutPutAns(dp[st].pre);
    printf("%s\n", a[dp[st].key].name); } int main() { int T, n;
    scanf("%d", &T); while (T--) {
        scanf("%d", &n); for (int i = 0; i < n; i++)
            scanf("%s %d %d", a[i].name, &a[i].dealine, &a[i].time); for (int i = 0; i <= 1 << n; i++)
            dp[i].min_re_s = oo;
        dp[0].min_re_s = 0;
        dp[0].time = 0;
        dp[0].pre = -1;
        dp[0].key = 0;
        mark[0] = 1; int allst = (1 << n) - 1; for (int i = 0; i <= allst; i++) { for (int j = 0; j < n; j++) { if (i&(1 << j)) continue; int st = i | (1 << j); int time = dp[i].time + a[j].time; int min_re_s = time - a[j].dealine; if (min_re_s < 0)
                    min_re_s = 0; if (min_re_s + dp[i].min_re_s < dp[st].min_re_s) {
                    dp[st].min_re_s = min_re_s + dp[i].min_re_s;
                    dp[st].pre = i;
                    dp[st].time = time;
                    dp[st].key = j; } } }
        printf("%d\n", dp[allst].min_re_s);
        OutPutAns(allst); } return 0; }

你可能感兴趣的:(dp,HDU)