标签:note none follow math long algo 题目 lin hat
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1074
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10195 Accepted Submission(s): 4900
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 15+10; 19 20 struct node 21 { 22 string name; 23 int deadline, cost; 24 }subject[MAXN]; 25 int dp[1<<16], id[1<<16], pre[1<<16]; 26 27 void Print(int s) 28 { 29 if(!s) return; 30 Print(pre[s]); 31 cout<< subject[id[s]].name <<endl; 32 } 33 34 int main() 35 { 36 int T, n; 37 scanf("%d", &T); 38 while(T--) 39 { 40 scanf("%d", &n); 41 for(int i = 0; i<n; i++) 42 cin>>subject[i].name>>subject[i].deadline>>subject[i].cost; 43 44 memset(dp, -1, sizeof(dp)); 45 dp[0] = 0; 46 for(int s = 0; s<(1<<n); s++) 47 { 48 int time = 0; 49 for(int i = 0; i<n; i++) 50 if(s&(1<<i)) 51 time += subject[i].cost; 52 53 for(int i = 0; i<n; i++) 54 { 55 if(!(s&(1<<i))) 56 { 57 int exceeded = time + subject[i].cost - subject[i].deadline; 58 if(exceeded<0) exceeded = 0; 59 int tmp = s|(1<<i); 60 if(dp[tmp]==-1 || dp[tmp]>dp[s]+exceeded) 61 { 62 dp[tmp] = dp[s] + exceeded; 63 id[tmp] = i; 64 pre[tmp] = s; 65 } 66 } 67 } 68 } 69 70 printf("%d\n", dp[(1<<n)-1]); 71 Print((1<<n)-1); 72 } 73 }
标签:note none follow math long algo 题目 lin hat
原文地址:http://www.cnblogs.com/DOLFAMINGO/p/7631252.html