Hdu1074 --- Doing Homework, hdu1074 --- doing

Source: Internet
Author: User

Hdu1074 --- Doing Homework, hdu1074 --- doing

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 specified CED 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 shocould output the smallest total CED 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

Simple state compression dp

/*************************************** * *********************************> File Name: dp6.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: thursday, February 12, 2015 ******************************** **************************************** /# include <map> # include <set> # include <queue> # include <stack> # include <vector> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstring> # Include <iostream> # include <algorithm> using namespace std; const double pi = acos (-1); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair <int, int> PLL; const int N = (1 <15) + 100; int dp [N]; char str [100]; map <int, string> _ hash; int n; struct node {int deadline; int cost;} course [20]; void dfs (int sta) {if (! Sta) {return;} int next; for (int I = n-1; I> = 0; -- I) {if (! (Sta & (1 <I) {continue;} int tmp = sta ^ (1 <I); int cost = 0; for (int j = 0; j <n; ++ j) {if (tmp & (1 <j) {cost + = course [j]. cost ;}} int use = course [I]. cost + cost-course [I]. deadline; if (use <0) {use = 0;} if (dp [sta] = dp [tmp] + use) {next = I; break ;}} dfs (sta ^ (1 <next); printf ("% s \ n", _ hash [next]. c_str ();} int main () {int t; scanf ("% d", & t); while (t --) {memset (dp, inf, sizeof (dp); dp [0] = 0; _ hash. clear (); scanf ("% d", & n); for (int I = 0; I <n; ++ I) {scanf ("% s % d", str, & course [I]. deadline, & course [I]. cost); _ hash [I] = str ;}for (int I = 1; I <(1 <n); ++ I) {for (int j = 0; j <n; ++ j) {if (I & (1 <j) = 0) {continue ;} int tmp = (I ^ (1 <j); int x = 0; int cost = 0; for (int k = 0; k <n; ++ k) {if (tmp & (1 <k) {cost + = course [k]. cost; ++ x ;}} int use = cost + course [j]. cost-course [j]. deadline; if (use <0) {use = 0;} if (dp [I]> = dp [tmp] + use) {dp [I] = dp [tmp] + use ;}} printf ("% d \ n", dp [(1 <n)-1]); dfs (1 <n)-1);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.