Doing Homework

来源:互联网 发布:网络公关公司良心哥 编辑:程序博客网 时间:2024/06/03 23:07

Doing Homework

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 30   Accepted Submission(s) : 21
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
23Computer 3 3English 20 1Math 3 23Computer 3 3English 6 3Math 6 3
 

Sample Output
2ComputerMathEnglish3ComputerEnglishMath
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<queue>#include<math.h>#include<list>#include<string>#include<set>#include<stack>#include<vector>#include<iostream>#include<iterator>#include<algorithm>using namespace std;struct node{int time;//截止时间int fen;//完成所需时长char kemu[105];} str[15];struct node1{int qian;  //前一个状态int time;  // 完成时间int fen;   //扣分int a;     // 记录路径} dp[1 << 15], stu;int vis[1 << 15];int main(){int t;scanf("%d", &t);while (t--){int n;scanf("%d", &n);getchar();for (int i = 0; i<n; i++){scanf("%s %d %d", str[i].kemu, &str[i].time, &str[i].fen);}memset(vis, 0, sizeof(vis));dp[0].qian = -1;dp[0].time = 0;dp[0].fen = 0;dp[0].a = -1;vis[0] = 1;for (int i = 0; i<long(1 << n); i++){for (int j = 0; j<n; j++){if ((i&(1 << j)) == 0)  //  1<<j  位置的科目没有选{stu.qian = i;stu.time = dp[stu.qian].time + str[j].fen;  // 到达当前状态   i|(1<<j)  花的总时间stu.fen = stu.time - str[j].time;  //  完成该任务时的总时间—该任务的截止时间if (stu.fen<0) stu.fen = 0;  // 未到截止时间  不扣分stu.fen += dp[stu.qian].fen;stu.a = j;   // 该状态下完成的科目if (vis[i | (1 << j)]) //  当前状态之前出现{if (stu.fen<dp[i | (1 << j)].fen){dp[i | (1 << j)] = stu;}}else { dp[i | (1 << j)] = stu; vis[i | (1 << j)] = 1; }}}}stack<int>P;printf("%d\n", dp[(1 << n) - 1].fen);int k = (1 << n) - 1;for (int i = 0; i < n; i++){P.push(dp[k].a);k = dp[k].qian;}while (!P.empty()){k = P.top(); P.pop();printf("%s\n", str[k].kemu);}}return 0;}


0 0
原创粉丝点击