## 804 - Petri Net Simulation

Moderator: Board moderators

### 804 - Petri Net Simulation

I have solved the problem but getting WA.
Can anyone tell me what should be the output for the following input set...

Input:
Code: Select all
2
1 0
2
-1 2 0
-2 1 0
100
3
3 0 0
3
-1 2 0
-2 -2 3 0
-3 1 0
100
3
1 0 0
3
-1 2 3 0
-2 1 0
-3 1 0
1
3
3 0 0
3
-1 2 0
-2 -2 3 0
-3 1 0
9
3
3 0 0
3
-1 2 0
-2 -2 3 0
-3 1 0
10
3
3 0 0
3
-1 2 0
-2 -2 3 0
-3 1 0
8
0

My code returns
Output:
Code: Select all
Case 1: still live after 100 transitions
Places with tokens: 1 (1)

Case 2: dead after 9 transitions
Places with tokens: 2 (1)

Case 3: still live after 1 transitions
Places with tokens: 2 (1) 3 (1)

Case 4: still live after 9 transitions
Places with tokens: 2 (1)

Case 5: dead after 9 transitions
Places with tokens: 2 (1)

Case 6: still live after 8 transitions
Places with tokens: 1 (1)

Last edited by Jan on Tue Aug 23, 2005 12:26 pm, edited 1 time in total.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru

Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm

Finally got accepted.

The output given is correct...
Ami ekhono shopno dekhi...
HomePage
Jan
Guru

Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm

### How to make it fast?

People are solving this within a pretty small time limit........................I could get no better idea than brute force simulation.........Any idea?
imnew
New poster

Posts: 5
Joined: Sun Feb 12, 2006 6:37 am

I keep getting TLE for this problem too.
sclo
Guru

Posts: 519
Joined: Mon Jan 23, 2006 10:45 pm

The 10 sec time limit might be too tight for this problem.
sclo
Guru

Posts: 519
Joined: Mon Jan 23, 2006 10:45 pm

Brute force will do it. Remember that you can greedly get the first active transition you find. This should speed up. Also, if one of the imputs do not suffice, you can also discard that transition, since it's surely not active.
lpereira
New poster

Posts: 5
Joined: Wed Nov 16, 2005 7:35 pm
Location: Campinas - SP - Brazil

My program returns exactly the same output, but I still get "wrong answer". Has anybody an idea, what kind of reason this could have? (Is it important whether I print a blank line before/after output?)
@Jan: What did you do to get accepted?

p137
p137
New poster

Posts: 1
Joined: Mon Jul 10, 2006 9:54 am

p137 wrote:@Jan: What did you do to get accepted?

I just simulate the process efficiently. May be you have missed this line
The input data will be selected to guarantee the uniqueness of the correct output displays.

Hope it helps .
Ami ekhono shopno dekhi...
HomePage
Jan
Guru

Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm

### Re: 804 - Petri Net Simulation

I can't find any test cases that make this fail, but am still getting WA, any ideas?

Code: Select all
#include <stdio.h>

int main() {
int mi[120][120];
int mc[120][120];
int s[120];
int c, d, f, g, n, t;

int i, j, k;

scanf(" %d", &n);
g = 1;
while (n != 0) {
if (g > 1) putchar('\n');

for (i = 0; i < n; i++) {
scanf(" %d", &s[i]);
}
scanf(" %d", &t);

for (i = 0; i < n; i++) {
for (j = 0; j < t; j++) {
mc[i][j] = mi[i][j] = 0;
}
}

for (i = 0; i < t; i++) {
scanf("%d", &k);
while (k != 0) {
if (k < 0) {
mi[-k - 1][i]++;
mc[-k - 1][i]--;
} else {
mc[k - 1][i]++;
}

scanf("%d", &k);
}
}

scanf(" %d", &f);

c = 0;
while (c < f) {
d = 0;
for (i = 0; i < t; i++) {
for (j = 0; j < n; j++) {
if (s[j] < mi[j][i]) {
break;
}
}

if (j == n) {
for (j = 0; j < n; j++) {
s[j] += mc[j][i];
}

c++;
d = 1;
if (c == f) goto end;
}
}
if (!d) {
break;
}
}

end:
if (!d) {
printf("Case %d: dead after %d transitions\n", g++, c);
} else {
printf("Case %d: still live after %d transitions\n", g++, c);
}

printf("Places with tokens:");
for (i = 0; i < n; i++) {
if (s[i] > 0) {
printf(" %d (%d)", i + 1, s[i]);
}
}
putchar('\n');

scanf(" %d", &n);
}
return 0;
}

I've tried the set Jan sent, and it got the same answers. I also tried this (appended to Jan's set):
Code: Select all
2
2 0
2
-1 2 2 0
-2 -2 -2 1 0
10

2
2 0
2
-1 2 2 0
-2 -2 -2 1 0
6

expecting:
Code: Select all
Case 7: dead after 6 transitions
Places with tokens: 2 (2)

Case 8: still live after 6 transitions
Places with tokens: 2 (2)

and it worked, but the judge still gives me WA.