//UVA658It'sNotABug,It'sAFeature!
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 20;
const int maxm = 100 + 10;
const int INF = 1000000000;
char before[maxm][maxn + 5], after[maxm][maxn + 5];
int n, m;
int t[maxm], dis[1 << maxn];
bool vis[1 << maxn];
struct Node {
int bug, dist;
bool operator < (const Node& rhs) const {
return dist > rhs.dist;
}
};
int solve() {//Dijkstra
priority_queue<Node> q;
for(int i = 0; i < (1 << n); i++) {
vis[i] = 0;
dis[i] = INF;
}
Node start;
start.bug = (1 << n) - 1;
start.dist = 0;
dis[start.bug] = 0;
q.push(start);
while(!q.empty()) {
Node u = q.top(); q.pop();
if(vis[u.bug]) continue;
vis[u.bug] = 1;
if(u.bug == 0) return u.dist;
for(int i = 0; i < m; i++) {
bool op = true;
for(int j = 0; j < n; j++) {
if(before[i][j] == '+' && !(u.bug & (1 << j))) {
op = false; break;
}
if(before[i][j] == '-' && (u.bug & (1 << j))) {
op = false; break;
}
}
if(!op) continue;
Node u2 = u;
u2.dist = u.dist + t[i];
for(int j = 0; j < n; j++) {
if(after[i][j] == '+') u2.bug |= (1 << j);
if(after[i][j] == '-') u2.bug &= ~(1 << j);
}
int& d = dis[u2.bug];
if(dis[u2.bug] == INF || u2.dist < d) {
d = u2.dist;
q.push(u2);
}
}
}
return -1;
}
int main() {
int kase = 0;
while(scanf("%d%d", &n, &m) == 2 && n) {
//memset(vis, 0, sizeof(vis));
for(int i = 0; i < m; i++) scanf("%d%s%s", &t[i], before[i], after[i]);
int ans = solve();
printf("Product %d\n", ++kase);
if(ans < 0) printf("Bugs cannot be fixed.\n\n");
else printf("Fastest sequence takes %d seconds.\n\n", ans);
}
return 0;
}
/*
3 3
1 000 00-
1 00- 0-+
2 0-- -++
4 1
7 0-0+ ----
0 0
*/
UVA658It'sNotABug,It'sAFeature!
最新推荐文章于 2019-02-09 17:16:54 发布