Https://vjudge.net/contest/68966#problem/D
http://blog.csdn.net/u010489389/article/details/19218795
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <string>5#include <algorithm>6#include <cmath>7#include <queue>8 #defineINF 0x3f3f3f3f9 using namespacestd;Ten Const intmaxn= the; One intN; A structHmwk - { - CharCH[MAXN]; the intD; - intC; -}p[ -]; - + structDP - { + intTime ; A intreduced; at intFA; -}dp[(1<< the)]; - - Chars[ -][101]; - - voidInit () in { - for(intI=0;i< (1<< the); i++) to { +dp[i].fa=-1; -Dp[i].time=0; theDp[i].reduced=INF; * } $dp[0].reduced=0;Panax Notoginseng - } the //Sequential output by dictionary order + BOOLcmpConstHmwk&x,Consthmwk&y) A { the returnstrcmp (x.ch,y.ch) <=0; + } - //recursive output, X is current, FA is the previous one $ voidPrint (intXintFA) $ { - intFlag; - if(x==0) the { - for(intI=0; i<n;i++)Wuyi { the if(fa& (1<<i)) - { Wuflag=i; - Break; About } $ } -printf"%s\n", p[flag].ch); - return; - } A Print (dp[x].fa,x); + for(intI=0; i<n;i++) the { - if((x& (1<<i)! = (fa& (1<<i))) $ { theflag=i; the Break; the } the } - if(x==0) in { theprintf"%s\n", p[flag].ch); the } About Else the { theprintf"%s\n", p[flag].ch); the } + } - the intMain ()Bayi { the intT; thescanf"%d",&T); - while(t--) - { the thescanf"%d",&n); the for(intI=0; i<n;i++) the { -scanf"%s%d%d",p[i].ch,&p[i].d,&p[i].c); the } theSort (p,p+n,cmp); the Init ();94 //each I represents a state, each of which represents an account the for(intI=0;i< (1<<N); i++) the { the for(intk=0; k<n;k++)98 { About //if it's 1, skip. - if(i& (1<<k))101 {102 Continue;103 }104 //Now is the state after the transfer the intnow=i+ (1<<k);106dp[now].time=dp[i].time+p[k].c;107 //dp[now].time-p[k].d<0 no extension, that's 0.108 if(Dp[i].reduced+max (0, DP[NOW].TIME-P[K].D) <dp[now].reduced)109 { theDp[now].reduced=dp[i].reduced+max (0, dp[now].time-p[k].d);111Dp[now].fa=i; the } 113 } the } the //The end result is 111111 (n 1), the corresponding number is (1<<n)-1 theprintf"%d\n", dp[(1<<n)-1].reduced);117Print (dp[(1<<n)-1].FA, (1<<n)-1);118 }119 return 0; -}
View Code
"Algorithm Series learning" pressure DP [kuangbin take you to fly] topic 12 basic DP1 d-doing Homework