HDU 1074 Doing Homework(状压dp)

来源:互联网 发布:淘宝宝贝图片怎么修改 编辑:程序博客网 时间:2024/05/16 04:22

输出路径时要注意,越后面的作业越后输出,所以正序遍历时,一旦有成绩小于等于马上更新pre指针,那样的话就能保证做作业是字典序了。

////  main.cpp//  Richard////  Created by 邵金杰 on 16/9/27.//  Copyright © 2016年 邵金杰. All rights reserved.//#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=(1<<15);const int inf=99999999;int dp[maxn],ti[maxn],dead[15],cost[15],pre[maxn],now[maxn];string s[15];void dfs(int x){    if(x==0) return ;    dfs(pre[x]);    cout<<s[now[x]]<<endl;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        for(int i=0;i<n;i++) cin>>s[i]>>dead[i]>>cost[i];        for(int i=1;i<(1<<n);i++)        {            dp[i]=inf;            for(int j=0;j<n;j++)            {                int tmp=(1<<j);                if(i&tmp)                {                    int past=i-tmp;                    int score=ti[past]+cost[j]-dead[j];                    if(score<0) score=0;                    if(score+dp[past]<=dp[i])                    {                        dp[i]=dp[past]+score;                        pre[i]=past;                        now[i]=j;                        ti[i]=ti[past]+cost[j];                    }                }            }        }        cout<<dp[(1<<n)-1]<<endl;        dfs((1<<n)-1);    }    return 0;}


0 0
原创粉丝点击