标签:
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7543 Accepted Submission(s): 3375
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<stack> using namespace std; const int INF=1<<30; struct Node { char name[105]; int dead,cost; }cla[18]; struct DP { int pre,time,spent,now; }dp[1<<15]; int main() { int t,i,j,s,n,end; //scanf("%d",&t); cin>>t; while(t--) { memset(dp,0,sizeof(dp)); //scanf("%d",&n); cin>>n; for(int i=0;i<n;i++) //scanf("%s%d%d",cla[i].name,&cla[i].dead,&cla[i].cost); cin>>cla[i].name>>cla[i].dead>>cla[i].cost; int en=1<<n; for(int s=1;s<en;s++) { dp[s].spent=INF; for(int i=n-1;i>=0;i--) { int tem=1<<i; if(s&tem) { int past=s-tem; int st=dp[past].time+cla[i].cost-cla[i].dead; if(st<0) st=0; if(dp[s].spent>dp[past].spent+st) {dp[s].spent=dp[past].spent+st; dp[s].now=i; dp[s].pre=past; dp[s].time=dp[past].time+cla[i].cost; } } } } stack<int> S; int tem = en-1; cout << dp[tem].spent << endl; while(tem) { S.push(dp[tem].now); tem = dp[tem].pre; } while(!S.empty()) { cout << cla[S.top()].name << endl; S.pop(); } } return 0; }
HDU_1074_Doing Homework_状态压缩dp
标签:
原文地址:http://www.cnblogs.com/jasonlixuetao/p/5468605.html