#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int maxn = 105;
int token[maxn],n,m,bound;
struct Rules {
map<int, int> in,out;
}rules[maxn];
bool RunOnce() {
for (int i = 1; i <= m; ++i) {
bool isok = true;
for (auto it = rules[i].in.begin(); it != rules[i].in.end(); ++it) {
if (token[it->first] < it->second) {
isok = false;
break;
}
}
if (isok) {
for (auto it = rules[i].in.begin(); it != rules[i].in.end(); ++it) {
token[it->first] -= it->second;
}
for (auto it = rules[i].out.begin(); it != rules[i].out.end(); ++it) {
token[it->first] += it->second;
}
return true;
}
}
return false;
}
int main()
{
int t,cnt,id = 0;
while (cin >> n && n) {
for (int i = 1; i <= n; ++i)
cin >> token[i];
cin >> m;
for (int i = 1; i <= m; ++i) {
rules[i].in.clear();
rules[i].out.clear();
while (cin >> t && t) {
if (t < 0) {
rules[i].in[-t]++;
}
else {
rules[i].out[t]++;
}
}
}
cin >> bound;
for (cnt = 1; cnt <= bound; ++cnt) {
if(!RunOnce()) break;
}
if (cnt <= bound) cout << "Case " << ++id << ": dead after " << cnt-1 << " transitions" << endl;
else cout << "Case " << ++id << ": still live after " << bound << " transitions" << endl;
cout << "Places with tokens:";
for (int i = 1; i <= n; ++i) {
if (token[i]) {
cout << " " << i << " (" << token[i] << ")";
}
}
cout << endl << endl;
}
return 0;
}
习题6-7(uva-804)
最新推荐文章于 2021-08-02 16:42:07 发布