hdu 1074
2014-07-23 08:57
232 查看
又是状态压缩的dp。
要输出路径,那只要记录每个状态的前一个状态,就能那样递归输出了。
贴代码。
要输出路径,那只要记录每个状态的前一个状态,就能那样递归输出了。
贴代码。
#include<iostream> #include<string> #include<stack> using namespace std; struct node{ string name; int d; //截止日期 int c; //需要花费日期 }hw[20]; struct point{ int now,pre; //当前状态id,与过去状态id int now_time; //当前时间 int score; //当前最少花费 int key; //当前课程的id }dp[40000]; int main(){ int t,n,i,j; cin>>t; while(t--){ cin>>n; for(i=0;i<n;i++) cin>>hw[i].name>>hw[i].d>>hw[i].c; dp[0].now_time=0; dp[0].score=0; dp[0].now=0; for(i=1;i<(1<<(n));i++){ dp[i].score=1000000000; for(j=n-1;j>=0;j--){ if(i&(1<<j)){ int pre_id=i-(1<<j),temp=0; if(dp[pre_id].now_time+hw[j].c>hw[j].d) temp=dp[pre_id].now_time+hw[j].c-hw[j].d; else temp=0; if(dp[pre_id].score+temp<dp[i].score){ dp[i].score=dp[pre_id].score+temp; dp[i].now=i; dp[i].pre=pre_id; dp[i].now_time = dp[pre_id].now_time+hw[j].c; dp[i].key=j; } } } } stack<string >st; cout<<dp[(1<<n)-1].score<<endl; int t=(1<<(n))-1; while(dp[t].now!=0){ st.push(hw[dp[t].key].name); t=dp[t].pre; // cout<<t<<endl; } while(!st.empty()){ cout<<st.top()<<endl; st.pop(); } } return 0; }
相关文章推荐
- HDU 1074 The Highest Mark(状压DP)
- HDU 1074 Doing Homework (状压dp)
- hdu 1074 Doing Homework
- HDU 1074 Doing Homework【状态压缩DP】
- hdu1074 状态压缩dp 记录路径
- hdu 1074 Doing Homework(状压DP)
- dp + dfs 状态保存(重点) hdu 1074 sdut 1356
- hdu 1074
- HDU-1074 Doing Homework( 状压DP )
- HDU 1074 Doing Homework(状态压缩DP)
- HDU 1074
- HDU 1074 Doing Homework
- 状态压缩DP-HDU-1074-Doing Homework
- hdu 1074 Doing Homework (状压dp)
- 【HDU 1074 Doing Homework】+ 状压dp
- hdu 1074 Doing Homework (状态压缩 dp)
- HDU 1074 Doing Homework(像缩进DP)
- hdu 1074 Doing Homework(状态压缩dp)
- hdu 1074 doing homework
- hdu1074 状态压缩dp 记录路径