https://vjudge.net/contest/372814#problem/E

https://vjudge.net/contest/372814#problem/E

n=15考虑状压dp

 

 

 

 

#include <bits/stdc++.h>
#define inf 2333333333333333
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(long long i=a;i<=b;++i)

using namespace std;
long long T,n,tot,temp,ki,pre_pt,cur_pt;
long long f[1<<16],t[1<<16],pre[1<<16];

struct node{
    string name;
    long long ddl;
    long long t;
}a[100];
stack<node>st;

void in(long long &x){
    long long y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(long long x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

void init(){
    memset(pre,0,sizeof(pre));
    memset(f,0,sizeof(f));
    memset(t,0,sizeof(t));
}

signed main(){
    in(T);
    while(T--){
        init();
        in(n);
        For(i,1,n){
            cin>>a[i].name;
            in(a[i].ddl);
            in(a[i].t);
        }
        tot=(1<<n)-1;
        For(i,0,tot) f[i]=inf;
        f[0]=0;
        For(S,1,tot){
            for(long long i=n-1;i>=0;i--){
                if(S&(1<<i)){
                    temp=S^(1<<i);
                    ki=max((long long)0,t[temp]+a[i+1].t-a[i+1].ddl);
                    if(f[temp]+ki<f[S]){
                        f[S]=f[temp]+ki;
                        pre[S]=temp;
                        t[S]=t[temp]+a[i+1].t;
                    }
                }
            }
        }
        For(i,1,n+1){
            if(i==1){
                pre_pt=tot;
            }
            else{
                cur_pt=pre[pre_pt];
                temp=cur_pt^pre_pt;
                For(j,0,n-1){
                    if((1<<j)&temp)
                        st.push(a[j+1]);
                }
                pre_pt=cur_pt;
            }
        }
        o(f[tot]);p('\n');
        while(!st.empty()){
            cout<<st.top().name<<endl;
            st.pop();
        }
    }
    return 0;
}

 

posted @ 2020-05-12 19:00  WeiAR  阅读(849)  评论(0编辑  收藏  举报