您的位置:首页 > 其它

hdu-1074 Doing Homework [状态dp]

2016-03-08 20:27 381 查看
D - Doing Homework
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
SubmitStatus

Description

Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce
his score of the final test, 1 day for 1 point. And as you know, doing homework always takes a long time. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.

Each test case start with a positive integer N(1<=N<=15) which indicate the number of homework. Then N lines follow. Each line contains a string S(the subject's name, each string will at most has 100 characters) and two integers D(the deadline of the subject),
C(how many days will it take Ignatius to finish this subject's homework).

Note: All the subject names are given in the alphabet increasing order. So you may process the problem much easier.

Output

For each test case, you should output the smallest total reduced score, then give out the order of the subjects, one subject in a line. If there are more than one orders, you should output the alphabet smallest one.

Sample Input

2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3


Sample Output

2
Computer
Math
English
3
Computer
English
Math


Hint

In the second test case, both Computer->English->Math and Computer->Math->English leads to reduce 3 points, but the word "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order.


这道题网上讲解很多,我就直接代码奉上了

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <stack>
#define MAX_SIZE 20
#define MAX_STATUS 1<<17
using namespace std;
struct subject{
int deadline,cost;
};
vector<subject> sj;
int dp[MAX_STATUS];
int pre[MAX_STATUS];//记录当前状态的前一个状态
char in_str[MAX_SIZE][MAX_SIZE];
void init()
{//初始化
sj.clear();
pre[0]=-1;
memset(dp,0x3f,sizeof(dp));
}
void input(int n)
{//题目给出 读入字符串序列的是字典序
int deadline,cost;
for(int i=0;i<n;++i)
{
scanf("%s %d %d",&in_str[i],&deadline,&cost);
sj.push_back((subject){deadline,cost});
}
}
void solve(int n)
{
int end =(1<<n)-1;//结束状态为n个1
dp[0]=0;
for(int i=0;i<end;++i)
{/*顺序遍历所有状态。可以画来看看,每个状态
的前驱都访问到了,这个状态才会访问到它的转移目标,所以不用担心
顺序遍历会跳层
*/
for(int j=0;j<n;++j)//做下一个作业
if(!((1<<j)&i))
{
int cost=0;//总共花费时间
int next=(1<<j)|i;//下一个状态
for(int k=0;k<n;++k)
if((1<<k)&next)
cost+=sj[k].cost;
//计算扣分
int reduced=(cost<=sj[j].deadline?0:cost-sj[j].deadline);
//printf("i:%d dp:%d re:%d next:%d\n",i,dp[i],reduced,next);
if(dp[next]>dp[i]+reduced)
{
dp[next]=dp[i]+reduced;
pre[next]=i;
}
}
}
printf("%d\n",dp[end]);
//寻根存值
stack<int> sta;
for(int s=end;s!=0;s=pre[s])
sta.push(s^pre[s]);
while(!sta.empty())
{
int print=sta.top();sta.pop();
for(int i=0;i<n;++i)
if((1<<i)&print)
printf("%s\n",in_str[i]);
}
}
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d",&n);
input(n);
solve(n);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: