标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7070 Accepted Submission(s): 3104
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; const int MAXN=16; const int INF=0x3fffffff; struct Node{ char s[105]; int time,cost; Node() { memset(s,0,sizeof(s)); } }a[MAXN]; int n; int dp[1<<MAXN]; int pre[1<<MAXN]; void OutPut(int status) { if(status==0) return ; int t=0; for(int i=0;i<n;i++) if((status&(1<<i))!=0&&(pre[status]&(1<<i))==0) { t=i; break; } OutPut(pre[status]); printf("%s\n",a[t].s); } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%s",a[i].s); scanf("%d%d",&a[i].time,&a[i].cost); } for(int i=0;i<(1<<MAXN);i++) dp[i]=INF; memset(pre,0,sizeof(pre)); dp[0]=0; for(int st=0;st<(1<<n);st++) { int w=0; for(int i=0;i<n;i++) { if(st&(1<<i)) w+=a[i].cost; } for(int i=0;i<n;i++) { if((st&(1<<i))==0) { if(dp[st|(1<<i)]>dp[st]+max(0,w+a[i].cost-a[i].time)) { dp[st|(1<<i)]=dp[st]+max(0,w+a[i].cost-a[i].time); pre[st|(1<<i)]=st; } } } } printf("%d\n",dp[(1<<n)-1]); OutPut((1<<n)-1); } return 0; }
标签:
原文地址:http://www.cnblogs.com/program-ccc/p/5224200.html