HDU

来源:互联网 发布:神机妙算软件定额安装 编辑:程序博客网 时间:2024/06/01 14:15

状态压缩dp..

题目:
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test, 1 day for 1 point. And as you know, doing homework always takes a long time. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=15) which indicate the number of homework. Then N lines follow. Each line contains a string S(the subject’s name, each string will at most has 100 characters) and two integers D(the deadline of the subject), C(how many days will it take Ignatius to finish this subject’s homework).

Note: All the subject names are given in the alphabet increasing order. So you may process the problem much easier.
Output
For each test case, you should output the smallest total reduced score, then give out the order of the subjects, one subject in a line. If there are more than one orders, you should output the alphabet smallest one.
Sample Input
2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3
Sample Output
2
Computer
Math
English
3
Computer
English
Math

思路:
刚拿到题就懵了,感觉自己的写的题的数量太少。
还是看的网上的思路,然后自己理解了一下。这个题如果 n <= 10,那还可以暴力。。但是15!决定了根本不可能。
状态压缩dp

//这里的dp[i]指的是,i先转换成二进制,第k位若是1,则做了这件事,若是0,则没有//在这种状态下,比如dp[7].v代表做111,三件事都做了的最小的罚时,dp[3].v表示011这种情况最小的罚时//111肯定是要考虑 先 011 101 011 这三种情况最优的罚时+安排最后一件事的罚时,//可以发现,它的子问题转换成的二进制肯定是比它自己要小的//可以直接从1 枚举到 1<<n-1 ,这样它的子问题一定比它先处理,结果已经出来了。//推出状态转移方程 dp[i].v = max(dp[past].v + s) ; s是安排到最后一件事的罚时,dp[past].v是子问题最优解。#include <cstdio>#include <cstring>#include <iostream>#include <map>#include <stack>using namespace std;const int inf = 0x3f3f3f3f;const int maxn = 1 << 15;const int maxN = 20;struct DP{    int v,use,pre,last;    DP(int a,int b,int d,int c = -1)    {   v = a; use = b; pre = c; last = d;}    DP(){}}dp[maxn];struct node{    char s[105];    int dead,con;    node(char a[105],int b,int c)    {        for(int i = 0; i < 105; i++)            s[i] = a[i];        dead = b;   con = c;    }    node(){}}data[maxN];int main(){    int t,n;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        char s[105];        int a,b;        for(int i = 1; i <= n ; i++)        {            scanf("%s%d%d",s,&a,&b);            data[i] = node(s,a,b);        }        int iend = 1 << n;        dp[0].v = 0; dp[0].pre = -1; dp[0].use = 0;        for(int i = 1; i < iend; i++)           //每一种状态 0表示没做,1表示做了        {            dp[i].v = inf;            dp[i].pre = -1;            for(int j = n; j >= 1 ; j--)        //顺序问题不能倒过来,按字典序小的输出,                                                //所以要先判断输入较迟的事在后面做            {                int k = 1<<(j-1);                               if( i & k )                     //表示第j件事在这个状态里                {                    int past = i-k,s;             //s是安排第j件事的罚时                    if(dp[past].use + data[j].con - data[j].dead > 0)                        s =  dp[past].use + data[j].con - data[j].dead;                    else s = 0;                    if(s + dp[past].v < dp[i].v )                   //状态转移方程,dp[i].v = max(dp[past].v + s);                     {                        dp[i].v = dp[past].v + s;                        dp[i].pre = past;                           //记录路径                        dp[i].last = j;                                 dp[i].use = dp[past].use + data[j].con;                    }               }            }            }        stack<int> S;        int t = iend - 1;        while(dp[t].pre != -1)        {            S.push(dp[t].last);            t = dp[t].pre;        }        printf("%d\n",dp[iend -1]);        while(!S.empty())        {            int temp = S.top(); S.pop();            printf("%s\n",data[temp].s);        }    }    return 0;}
0 0
原创粉丝点击