HDU 1074 狀態DP

來源:互聯網
上載者:User
Problem DescriptionIgnatius 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. 


InputThe 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. 


OutputFor 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. 


Sample Input

23Computer 3 3English 20 1Math 3 23Computer 3 3English 6 3Math 6 3
 


Sample Output

2ComputerMathEnglish3ComputerEnglishMathHintIn the second test case, both Computer->English->Math and Computer->Math->English leads to reduce 3 points, but the word "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order. 
 

這裡使用搜尋+剪枝

#include <iostream>#include <string>#include <string.h>#include <algorithm>using namespace std;const int MAXN = 32868;int pow[17] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384};int hash[MAXN];int temp[20]; //臨時解決方案bool used[20]; //int path[20];int ans,i,j,t,n;typedef struct Node{int cost,dead;char name[300];}Node;Node arr[20];//窮舉法 + 剪枝void dfs(int index, int cnt,int sum,int cur,int state){if(cnt == n){if(sum < ans){ans = sum;for(int i=0; i<n; i++){path[i] = temp[i];}return;}}//剪枝. 如當前的結果比以前算出的結果大,就直接返回if(hash[state] != -1 && sum > hash[state])return;if(hash[state] == -1 || hash[state] > sum){hash[state] = sum;}for(int k=0; k<n; k++){if(!used[k]){int tempCur,tempSum;if(cur + arr[k].cost > arr[k].dead){tempSum = sum + cur + arr[k].cost - arr[k].dead;}else{tempSum = sum;}tempCur = cur + arr[k].cost;used[k] = true;temp[cnt] = k; //記錄dfs(k, cnt+1, tempSum, tempCur, state + pow[k] );used[k] = false;}}}int main(){freopen("in.txt","r",stdin);cin >> t;while(t--){cin >> n;for(i=0; i<n; i++){scanf("%s %d %d", arr[i].name, &arr[i].dead, &arr[i].cost);}memset(hash,-1,sizeof(hash));memset(used,false,sizeof(used));ans = 0x3fffffff;dfs(-1,0,0,0,0);cout << ans << endl;for(i=0; i<n; i++){printf("%s\n",arr[ path[i] ].name);}}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.