hdu 1074 Doing Homework

Doing Homework

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


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.
 
 
 
 用time[i] 来表示i的二进制位为1的作业所构成的集合完作业所需要 的时间
 用dp[i] 来表示i的二进制位为1的作业所构成的集合完作业所扣的最少分数
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <string>
#include <stack>
using namespace std;

const int N = (1<<15)+10;

int n, ddl[20], ti[20];
string str[20];
int dp[N], tim[N], pre[N], now[N];

int main()
{
    //freopen("in","r",stdin);
    int T; scanf("%d",&T);
    while(T--) {
        scanf("%d", &n);
        for(int i=0;i<n;i++) {
            cin >> str[i];
            scanf("%d %d", &ddl[i], &ti[i]);
        }
        memset(dp,0x3f,sizeof(dp));
        memset(tim,0,sizeof(tim));
        memset(pre,0,sizeof(pre));
        memset(now,0,sizeof(now));
        int end = (1<<n);
        dp[0] = 0;
        for(int s=1; s<end; s++) {
            for(int i=0; i<n; i++) {
                if((s>>i)&1) {
                    int preState = s - (1<<i);
                    int ans = max(0, tim[preState] + ti[i] - ddl[i]);
                    if(dp[preState] + ans <=  dp[s]) {
                        dp[s] = dp[preState] + ans;
                        tim[s] = tim[preState] + ti[i];
                        now[s] = i;
                        pre[s] = preState;
                    } 
                }    
            }
        }
        stack<int> st;
        int ed = (1<<n)-1;
        while (ed != 0) {
            st.push(now[ed]);
            ed = pre[ed];
        }
        cout << dp[end-1] <<endl;
        while (!st.empty()) {
            cout << str[st.top()]<<endl;
            st.pop();
        }
    }
    return 0;
}

 

posted @ 2018-10-09 15:27  Draymonder  阅读(231)  评论(0编辑  收藏  举报