HDU 1074 Doing Homework 状态压缩 + 搜索
题意:
就是有0-15个工作,每个工作有完成日期限制,还有完成这个工作所需要时间,求完成所有工作花时最少,并且输出这些顺序。。。
因为工作很小,最多也就1<<15种方案,BFS枚举所有方案即可。
View Code
/* 鐘舵€佸帇缂?+ 鎼滅储 */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<algorithm> using namespace std; struct node { char name[110]; int end; int day; }Work[20]; int N; struct Qnode { int cost; //鏈€灏戞崯澶卞垎鏁? int v;//鍓嶇姸鎬佸€? int work; //宸ヤ綔澶╂暟 char s[20]; }dp[40000]; void bfs( ) { queue<Qnode>q; int state, cost, day; Qnode xx, yy; xx.cost = xx.v = xx.work = 0; xx.s[0] = '\0'; dp[0] = xx; q.push(xx); while( !q.empty() ) { xx = q.front( ); state = xx.v; cost = xx.cost; day = xx.work; q.pop( ); for( int i = 0; i < N; i++) { if( ((1<<i)&state) == 0 ) //濡傛灉璇ヨ鏈闂? { yy.v = (1<<i)|state; yy.work = day + Work[i].day; yy.cost = cost; if( yy.work > Work[i].end ) yy.cost += yy.work - Work[i].end; if( dp[yy.v].cost == -1 || dp[yy.v].cost > yy.cost ) { strcpy(yy.s, xx.s); int len = strlen(yy.s); yy.s[len] = i + '0'; yy.s[len+1] = '\0'; dp[yy.v] = yy; q.push(yy); } } } } } int main( ) { int T; scanf("%d",&T); while(T--) { memset(dp, 0, sizeof(dp)); scanf("%d",&N); for( int i = 0; i < N; i++) { scanf("%s%d%d",Work[i].name, &Work[i].end, &Work[i].day); } for( int i = 0; i < (1<<N); i++) dp[i].cost = -1; bfs( ); printf("%d\n", dp[(1<<N)-1].cost); for( int i = 0; i < N; i++) printf("%s\n", Work[dp[(1<<N)-1].s[i] - '0'].name); } return 0; }
posted on 2012-08-05 09:42 more think, more gains 阅读(136) 评论(0) 编辑 收藏 举报