題目鏈接:uva 690 - Pipeline Scheduling
題目大意:有10個任務,5個管道,每個任務需要占用不同時間的管道,給出任務所占用管道的時間,求最短需要多少時間。
解題思路:dfs+剪枝,剪枝1,將所有可以的相對位置記錄。剪枝2,當當前開銷加上剩余任務的最有情況仍大於ans。
#include#include #include using namespace std; const int N = 5; const int M = 100; int n, c, ans, w[N], jump[M]; bool judge (int* s, int k) { for (int i = 0; i < N; i++) { if ((s[i]>>k)&w[i]) return false; } return true; } void init () { char str[M]; c = 0; ans = n * 10; memset(w, 0, sizeof(w)); for (int i = 0; i < N; i++) { scanf("%s", str); for (int j = 0; j < n; j++) if (str[j] == 'X') w[i] |= (1< ans) return; if (d == 10) { ans = min (ans, sum); return; } for (int i = 0; i < c; i++) { if (judge (s, jump[i])) { int p[N]; for (int j = 0; j < N; j++) p[j] = (s[j]>>jump[i])^w[j]; dfs (p, d + 1, sum + jump[i]); } } } int main () { while (scanf("%d", &n), n) { init (); dfs (w, 1, n); printf("%d\n", ans); } return 0; }