-
Notifications
You must be signed in to change notification settings - Fork 5
/
UVA-804.cpp
67 lines (65 loc) · 1.47 KB
/
UVA-804.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<stdio.h>
#include<string.h>
#include<map>
using namespace std;
struct NtNode {
map<int, int> in;
map<int, int> out;
};
int main() {
int n = 0, np, nt, nf;
int i ,j, k;
bool flagDead, flagTran;
int num;
while(scanf("%d", &np) == 1 && np > 0) {
int npArr[110];
NtNode ntArr[110];
++n;
for(i = 1; i <= np; ++i) {
scanf("%d", &npArr[i]);
}
scanf("%d", &nt);
for(i = 1; i <= nt; ++i) {
while(scanf("%d", &k) == 1 && k != 0) {
if(k > 0) ntArr[i].out[k] = ntArr[i].out[k] + 1;
else ntArr[i].in[-k] = ntArr[i].in[-k] + 1;
}
}
scanf("%d", &nf);
num = 0;
while(1) {
flagDead = false;
for(i = 1; i <= nt; ++i) {
flagTran = true;
for(auto ip = ntArr[i].in.begin(); ip != ntArr[i].in.end(); ++ip) {
if(npArr[ip->first] < ip->second) flagTran = false;
}
if(!flagTran) continue;
for(auto ip = ntArr[i].in.begin(); ip != ntArr[i].in.end(); ++ip) {
npArr[ip->first] -= ip->second;
}
for(auto ip = ntArr[i].out.begin(); ip != ntArr[i].out.end(); ++ip) {
npArr[ip->first] += ip->second;
}
flagDead = true;
++num;
if(num == nf) break;
}
if(num == nf || !flagDead) break;
}
printf("Case %d: ", n);
if(num != nf) {
printf("dead after %d transitions\n", num);
} else {
printf("still live after %d transitions\n", num);
}
printf("Places with tokens:");
for(i = 1; i <= np; ++i) {
if(npArr[i]) {
printf(" %d (%d)", i, npArr[i]);
}
}
printf("\n\n");
}
return 0;
}