码迷,mamicode.com
首页 > 其他好文 > 详细

状态压缩 HDU1074

时间:2016-12-02 13:46:41      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:push   nbsp   algorithm   har   time   print   ace   log   empty   

t组数据

n门课程 底限 完成要几天

dp[i] 表示i的二进制数中  1 对应位置课程 完成  最少扣多少分 完成的时间 记录一下怎么下来的

1->2^n 列举

(1<<n)   -1 就是全部都是1  然后输出即可

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stack>

using namespace std;

#define inf 100000000

struct node
{
    char z[100];
    int da,ta;

}x[20];
struct node1
{
    int time,pa,sc,now;

}dp[1<<16];

bool cmp(node a,node b)
{
    return strcmp(a.z,b.z)<0;
}
stack<int>s;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%s%d%d",x[i].z,&x[i].da,&x[i].ta);
        sort(x,x+n,cmp);
        int en=1<<n;

        for(int i=1;i<=en;i++)
        {
            dp[i].sc=inf;
            for(int j=n-1;j>=0;j--)
            {
                int tem=1<<j;
                if(i&tem)
                {
                    int past=i-tem;
                    int st=dp[past].time+x[j].ta-x[j].da;
                    if(st<0)
                        st=0;
                    if(st+dp[past].sc<dp[i].sc)
                    {
                        dp[i].sc=st+dp[past].sc;
                        dp[i].pa=past;
                        dp[i].now=j;
                        dp[i].time=dp[past].time+x[j].ta;
                    }
                }
            }
        }

        en--;
        printf("%d\n",dp[en].sc);
        while(en)
        {
            s.push(dp[en].now);
            en=dp[en].pa;
        }
        while(!s.empty())
        {
            int now=s.top();
            printf("%s\n",x[now].z);
            s.pop();
        }
    }

    return 0;
}

 

状态压缩 HDU1074

标签:push   nbsp   algorithm   har   time   print   ace   log   empty   

原文地址:http://www.cnblogs.com/cherryMJY/p/6125256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!