题目链接如下:
我的代码如下:
#include <cstdio>
#include <map>
#include <algorithm>
const int maxx = 100;
// #define debug
int np, nt, nf, t, kase = 0;
int token[maxx];
std::map<int, int> input[maxx], output[maxx];
std::map<int, int> empty;
bool canFire(int k){
for (auto it = input[k].begin(); it != input[k].end(); ++it){
if (it->second > token[it->first]){
return false;
}
}
return true;
}
int fireTransition(){
for (int i = 1; i <= nt; ++i){
if (canFire(i)){
return i;
}
}
return -1;
}
int main(){
#ifdef debug
freopen("0.txt", "r", stdin);
freopen("1.txt", "w", stdout);
#endif
while (scanf("%d", &np) == 1 && np){
int i;
for (i = 1; i <= np; ++i){
scanf("%d", &token[i]);
}
scanf("%d", &nt);
std::fill(input, input + nt + 1, empty);
std::fill(output, output + nt + 1, empty);
for (i = 1; i <= nt; ++i){
while (scanf("%d", &t) == 1 && t) {
if (t > 0){
output[i][t]++;
} else {
input[i][-t]++;
}
}
}
scanf("%d", &nf);
for (i = 0; i < nf; ++i){
int tmp = fireTransition();
if (tmp == -1){
break;
}
for (auto it = input[tmp].begin(); it != input[tmp].end(); ++it){
token[it->first] -= it->second;
}
for (auto it = output[tmp].begin(); it != output[tmp].end(); ++it){
token[it->first] += it->second;
}
}
printf("Case %d: ", ++kase);
printf("%s %d transitions\nPlaces with tokens:", i == nf ? "still live after" : "dead after", i);
for (i = 1; i <= np; ++i){
if (token[i]){
printf(" %d (%d)", i, token[i]);
}
}
printf("\n\n");
}
#ifdef debug
fclose(stdin);
fclose(stdout);
#endif
return 0;
}