HDU 1075 Doing homework 动态规划状态压缩

来源:互联网 发布:佛山新城网络联系方式 编辑:程序博客网 时间:2024/05/21 22:57

原题:HDU-1075


#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <stack>using namespace std;struct homework {    char name[105];    int deadline;    int time;} h[20];struct statu {    int score;    int now;    int pre;    int time;} dp[1<<15];int main() {    int N,n;    scanf("%d",&N);    while(N--) {        memset(dp,0,sizeof(dp));        scanf("%d",&n);        for(int i=0; i<n; i++)           scanf("%s %d %d",h[i].name,&h[i].deadline,&h[i].time);  //保存各科作业的数据        int end=1<<n;                           // 用二进制进行枚举1<<n种的状态        for(int s=1; s<end; s++) {            dp[s].score=1<<30;            for(int i=n-1; i>=0; i--) {                int p=1<<i;                if(p&s) {                   //如果p&s等于1的话代表这门课是做了的状态                    int q=s-p;                    int f=dp[q].time+h[i].time-h[i].deadline;    //根据前一种状态的分数来计算现状态的分数                    if(f<0)f=0;                                 //分数如果小于0的话代表没有超过deadline 不用扣分                    if(f+dp[q].score<dp[s].score)               //取最小扣分 更新状态                    {                        dp[s].score=f+dp[q].score;                        dp[s].now=i;                              //记录路径                        dp[s].pre=q;                        dp[s].time=dp[q].time+h[i].time;                    }                }            }        }        stack<int>st;        int temp=end-1;        printf("%d\n",dp[temp].score);        while(temp){            st.push(dp[temp].now);            temp=dp[temp].pre;        }        while(!st.empty()){            int t=st.top();            st.pop();            printf("%s\n",h[t].name);        }    }    return 0;}


原创粉丝点击