当前位置: 代码迷 >> 综合 >> HDU 1074 Doing Homework 状态压缩+DP .
  详细解决方案

HDU 1074 Doing Homework 状态压缩+DP .

热度:116   发布时间:2023-09-23 06:14:50.0

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1074

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
struct HomeWork
{char name[100+5];int deadline,costday;
}homework[15+5];
struct Node
{int time,parent,score,idxN;
}d[1<<15];
void Print(int state){if(state==0) return;Print(d[state].parent);cout<<homework[d[state].idxN].name<<endl;
}
int main(int argc, char const *argv[])
{int T; cin>>T;while(T--){int n; cin>>n;for(int i=0;i<n;i++)scanf("%s%d%d",homework[i].name,&homework[i].deadline,&homework[i].costday);for(int state=1;state<(1<<n);state++){d[state].score=INF;for(int idx=0;idx<n;idx++){if(((1<<idx)&state)==0) continue;int pre=state-(1<<idx);int score=d[pre].time+homework[idx].costday-homework[idx].deadline;if(score<0) score=0;if (score+d[pre].score<=d[state].score)				{d[state].score=d[pre].score+score;d[state].parent=pre;d[state].idxN=idx;d[state].time=d[pre].time+homework[idx].costday;}}}cout<<d[(1<<n)-1].score<<endl;Print((1<<n)-1);}return 0;
}