您的位置:首页 > 其它

hdu1074(状压DP)

2014-11-27 16:20 211 查看
这题本来很简单,代码被我写搓了,字母翘错了一个一直不出结果。

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: