Doing Homework

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6967    Accepted Submission(s): 3043

Problem 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.

 
Author
Ignatius.L
 
思路:状压DP.
一开始想全排列,往这思路上想,这样谁都知道会超时,因为n<15;那么共有pow(2,n)的状态,那么状压dp可解决.
每个状态可由前面多个状态转移而来,所以取最小就是这个状态的最优解,局部最优,最后到整体最优。
状态转移方程和解释具体看下面代码
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 #include<stack>
8 #define sc(x) scanf("%I64d",&x)
9 #define pr(x) printf("%I64d",x);
10 #define prr(x) printf("%I64d\n",x);
11 #define prrr(x) printf(" %I64d",x);
12 #define FOR(i,p,q) for(int i=p;i<=q;i++)
13 int cmp(char *p,char *q);
14 typedef struct pp
15 {
16 int x;
17 int y;
18 char a[200];
19 } ss;
20 ss ab[20];
21 char bc[20][200];
22 const int N=1e9;
23 typedef struct qq
24 {
25 int time;
26 int pre;
27 int no;
28 int cost;
29 } kk;//结构体存这个状态的时间当前加入作业的编号,所罚的时间,和前一个状态
30 kk dp[1<<15+1];
31 using namespace std;
32 int main(void)
33 {
34 int n,i,j,k,p,q;
35 scanf("%d",&k);
36 while(k--)
37 {
38 scanf("%d",&n);
39 for(i=0; i<n; i++)
40 {
41 scanf("%s",ab[i].a);
42 scanf("%d %d",&ab[i].x,&ab[i].y);
43 }
44 for(i=0; i<(1<<15)+1; i++)
45 {
46 dp[i].cost=N;
47 }//初始化
48 dp[0].cost=0;
49 dp[0].pre=-1;
50 dp[0].time=0;//开始的时间等状态
51 for(i=1; i<(1<<n); i++)
52 {
53 for(j=0; j<n; j++)
54 {
55 if(i&(1<<j))//当前这个状态含有第j个作业
56 {
57 if(ab[j].x<=dp[i^(1<<j)].time)//找不含j的前一个状态,并用前一个状态结束时间与第j个作业截至时间比较,然后分情况讨论下
58 {
59 int cc=dp[i^(1<<j)].time-ab[j].x;
60 if(dp[i].cost>dp[i^(1<<j)].cost+cc+ab[j].y)
61 {
62 dp[i].cost=dp[i^(1<<j)].cost+cc+ab[j].y;
63 dp[i].pre=i^(1<<j);
64 dp[i].no=j;
65 dp[i].time=dp[i^(1<<j)].time+ab[j].y;
66 }
67 else if(dp[i].cost==dp[i^(1<<j)].cost+cc+ab[j].y)
68 {
69 if(cmp(ab[j].a,ab[dp[i].no].a)>0)//按字典序排序,将最大的放最后,因为没个都是两两比较
70 {
71 dp[i].cost=dp[i^(1<<j)].cost+cc+ab[j].y;
72 dp[i].pre=i^(1<<j);
73 dp[i].no=j;
74 dp[i].time=dp[i^(1<<j)].time+ab[j].y;
75 }
76 }
77 }
78 else
79 {
80 int uu=ab[j].y+dp[i^(1<<j)].time;
81 int cc=uu-ab[j].x;
82 if(cc<=0)
83 {
84 if(dp[i].cost>dp[i^(1<<j)].cost)
85 {
86 dp[i].cost=dp[i^(1<<j)].cost;
87 dp[i].pre=i^(1<<j);
88 dp[i].no=j;
89 dp[i].time=dp[i^(1<<j)].time+ab[j].y;
90 }
91 else if(dp[i].cost==dp[i^(1<<j)].cost)
92 {
93 if(cmp(ab[j].a,ab[dp[i].no].a)>0)
94 {
95 dp[i].cost=dp[i^(1<<j)].cost;
96 dp[i].pre=i^(1<<j);
97 dp[i].no=j;
98 dp[i].time=dp[i^(1<<j)].time+ab[j].y;
99 }
100 }
101
102 }
103 else
104 {
105 if(dp[i].cost>dp[i^(1<<j)].cost+cc)
106 {
107 dp[i].cost=dp[i^(1<<j)].cost+cc;
108 dp[i].pre=i^(1<<j);
109 dp[i].no=j;
110 dp[i].time=dp[i^(1<<j)].time+ab[j].y;
111 }
112 else if(dp[i].cost==dp[i^(1<<j)].cost+cc)
113 {
114 if(cmp(ab[j].a,ab[dp[i].no].a)>0)
115 {
116 dp[i].cost=dp[i^(1<<j)].cost+cc;
117 dp[i].pre=i^(1<<j);
118 dp[i].no=j;
119 dp[i].time=dp[i^(1<<j)].time+ab[j].y;
120 }
121 }
122
123 }
124
125 }
126 }
127 }
128 }
129 printf("%d\n",dp[(1<<n)-1].cost);
130 int pf=dp[(1<<n)-1].pre;
131 int zk=0;
132 while(zk<n-1)
133 {
134
135 strcpy(bc[zk],ab[dp[pf].no].a);
136 zk++;
137 pf=dp[pf].pre;
138 }
139 for(i=n-2;i>=0;i--)
140 {
141 printf("%s\n",bc[i]);
142 }printf("%s\n",ab[dp[(1<<n)-1].no].a);
143 }
144
145
146 }
147 int cmp(char *p,char *q)
148 {
149 return strcmp(p,q);
150 }

状压DP

Doing Homework(hdu)1074的更多相关文章

  1. (hdu)5391 Zball in Tina Town

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5391 Problem Description Tina Town is a friendl ...

  2. (hdu)1285 确定比赛名次

    Problem Description 有N个比赛队(<=N<=),编号依次为1,,,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接 ...

  3. Doing Homework(HDU 1074状压dp)

    题意:给你n个要做的作业,它们的名字.期限.可完成所需天数(必须连续)在规定期限不能完成要扣分(每天一分)求做作业顺序使扣分最少. 分析:作业数量较少,用状态压缩,做到第i种作业花费的天数dp[i]. ...

  4. (hdu)1042 N! 大数相乘

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1042 Problem Description Given an integer N( ≤ ...

  5. (hdu)5234 Happy birthday 二维dp+01背包

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5234 Problem Description Today is Gorwin’s birt ...

  6. (hdu)4858 项目管理 (vector)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4858 Problem Description 我们建造了一个大项目!这个项目有n个节点,用很多边连接起 ...

  7. 杭电(hdu)ACM 4548 美素数

    美素数 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submis ...

  8. 2019-2020 ICPC, Asia Jakarta Regional Contest A. Copying Homework (思维)

    Danang and Darto are classmates. They are given homework to create a permutation of N integers from  ...

  9. (hdu)1257 最少拦截系统

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1257 Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦 ...

随机推荐

  1. JAVA写入TXT

    用java生成txt文件有两种方式: 1)是通过字符流(或字节流): 2)是直接调用PrintWriter类. 具体实现过程如下: 1)字符流(字节流) 代码如下: import java.io.Fi ...

  2. 日常Java 2021/10/18

    Vecter类实现了一个动态数组,不同于ArrayList的是,Vecter是同步访问的, Vecter主要用在事先不知道数组的大小或可以改变大小的数组 Vecter类支持多种构造方法:Vecter( ...

  3. 大数据学习day36-----flume02--------1.avro source和kafka source 2. 拦截器(Interceptor) 3. channel详解 4 sink 5 slector(选择器)6 sink processor

    1.avro source和kafka source 1.1 avro source avro source是通过监听一个网络端口来收数据,而且接受的数据必须是使用avro序列化框架序列化后的数据.a ...

  4. pyqt5 的串口编写进度

    2020.12.18 今天遇到一个问题, 想用回车实现串口数据的发送. 下面这句话是让光标移动到文字的尾部,但是不能够实现. 对QTextEdit控件中的文字改写,或清除后,再调用下面的移动到尾部,就 ...

  5. android studio 报 Error:(79) Error parsing XML: not well-formed (invalid token)

    android studio 报 Error:(79) Error parsing XML: not well-formed (invalid token) 我的原因是因为string 里面有< ...

  6. Java——数组的定义与使用

    数组的定义与使用 1.数组的基本概念 (1)数组的动态初始化: 数组首先先开辟内存空间,而后再使用索引进行内容的设置,这种定义数组的方式称为动态初始化 数组是引用数据类型,存在有内存分配问题.在使用前 ...

  7. 实现nfs持久挂载+autofs自动挂载

    实验环境: 两台主机 node4:192.168.37.44 NFS服务器 node2:192.168.37.22 客户端 在nfs服务器,先安装nfs和rpcbind [root@node4 fen ...

  8. spring Profile 为不同环境提供不同的配置支持

    说明 Profile为在不同环境下使用不同的配置提供了支持(开发环境下的配置和生产环境下的配置肯定是不同的, 例如, 数据库的配置) . 在spring开发中用@Profile 注解使用来选择行配置系 ...

  9. 端口占用,windows下通过命令行查看和关闭端口占用的进程

    1.查找所有端口号对应的PID 端口号:8080 命令:netstat -ano|findstr "8080" 2.找到端口的PID并关闭 PID:1016 命令:taskkill ...

  10. 可落地的DDD代码实践

    目录 前言 一.从六边形架构谈起 二.依赖倒置 三.DDD 代码分层 3.1 用户接口层 3.2 应用层 3.2 1 Response vs Exception 3.2.2 CQE vs DTO 3. ...