您的位置:首页 > 其它

HDU 1074 Doing Homework 状态压缩+DP .

2016-10-04 21:47 543 查看
题目地址: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: