#include <vector> #include <cstdio> #include <cstring> using namespace std; int tokens[105]; vector<int> in[105], out[105]; int NP, NT, NF; int solve() { int count = 0, tran_i; for (tran_i = 0;tran_i < NT;tran_i++) { int flag = 0; for (int i = in[tran_i].size() - 1;i >= 0;i--) { if (tokens[in[tran_i][i]] > 0)tokens[in[tran_i][i]]--; else { for (int j = in[tran_i].size() - 1;j > i;j--) tokens[in[tran_i][j]]++; flag = 1; break; } } if (flag == 0) { for (int i = out[tran_i].size() - 1;i >= 0;i--) tokens[out[tran_i][i]]++; tran_i = -1; if (++count >= NF)return 0; } } return count; } void read() { int kase = 0; while (scanf("%d", &NP) == 1) { if (NP == 0)return; memset(tokens, 0, sizeof(tokens)); for (int i = 0;i < NP;i++) scanf("%d", tokens + i); scanf("%d", &NT); for (int i = 0;i < NT;i++) in[i].clear(), out[i].clear(); for (int i = 0;i < NT;i++) { int t; scanf("%d", &t); while (t) { if (t < 0)in[i].push_back(-t - 1);//看别人ac的代码,是把同一个输入库或者输出库统一,鄙人这种方法可能对大量重复数据比较吃力 else out[i].push_back(t - 1); scanf("%d", &t); } } scanf("%d", &NF); //printf("Case %d: ", ++kase); int c = solve(); if (c == 0)printf("Case %d: still live after %d transitions ", ++kase, NF); else printf("Case %d: dead after %d transitions ", ++kase, c); printf("Places with tokens:"); for (int i = 0;i < NP;i++) if (tokens[i])printf(" %d (%d)", i + 1, tokens[i]); printf(" "); } } int main(void) { read(); return 0; } //贴下别人的码 #include<cstdio> #include<cstring> #include<vector> using namespace std; const int maxn = 105; int p, t, np[maxn], lim; struct petri { int ip, op, i[maxn], o[maxn], in[maxn], out[maxn]; } pet[maxn]; int main() { int tt = 0; while (scanf("%d", &p), p) { memset(pet, 0, sizeof(pet)); for (int i = 1;i <= p;++i) scanf("%d", &np[i]); scanf("%d", &t); for (int i = 1;i <= t;++i) { int k; while (scanf("%d", &k), k) { if (k<0) ++pet[i].in[-k]; else ++pet[i].out[k]; } for (int j = 1;j <= p;++j) { if (pet[i].in[j]) pet[i].i[++pet[i].ip] = j; if (pet[i].out[j]) pet[i].o[++pet[i].op] = j; } } scanf("%d", &lim); int cnt = 0; for (int i = 1;i <= t;++i) { bool flag = true; petri &k = pet[i]; for (int j = 1;j <= k.ip;++j) if (np[k.i[j]]<k.in[k.i[j]]) { flag = false;break; } if (!flag) continue; for (int j = 1;j <= k.ip;++j) np[k.i[j]] -= k.in[k.i[j]]; for (int j = 1;j <= k.op;++j) np[k.o[j]] += k.out[k.o[j]]; i = 0; if (++cnt >= lim) break; } if (cnt >= lim) printf("Case %d: still live after %d transitions ", ++tt, lim); else printf("Case %d: dead after %d transitions ", ++tt, cnt); printf("Places with tokens:"); for (int i = 1;i <= p;++i) if (np[i]) printf(" %d (%d)", i, np[i]); printf(" "); } return 0; }