首页 技术 正文
技术 2022年11月22日
0 收藏 318 点赞 5,036 浏览 5038 个字

Input: petri.in A Petri net is a computational model used to illustrate concurrent activity. Each Petri net contains some number of places (represented by circles), transitions (represented by black rectangles),
and directed edges used to connect places to transitions, and transitions to places. Each place can hold zero or more tokens (represented by black dots). Here are two examples:

In the first Petri net above, there are two places (P1 and P2) and two transitions (T1 and T2). P1 initially has one token; P2 has none. P1 is an input place for transition T1, and P2 is an output place for T1.
In the second example there are three places and three transitions, with three tokens in P1. T2 has two input places, both of which are P2.

Operation of a Petri Net

Each transition in a Petri net is either enabled or disabled. A transition is enabled if there is at least one token in each of its input places. Any transition can fire whenever it is enabled. If multiple transitions
are enabled, any one of them may fire. When a transition fires, one token is removed from each of the input places, and one token is added to each of the output places; this is effectively done atomically, as one action. When there are no enabled transitions,
a Petri net is said to be dead.

In the top example only T1 is enabled. When it fires one token is removed from P1, and one token is added to P2. Then T2 is enabled. When it fires one token is removed from P2, and one token is added to P1. Clearly
this Petri net will repeat this cycle forever.

The bottom example is more interesting. T1 is enabled and fires, effectively moving a token to P2. At this point T1 is still the only enabled transition (T2 requires that P2 have two tokens before it is enabled).
T1 fires again, leaving one token in P1 and two tokens in P2. Now both T1 and T2 are enabled. Assume T2 fires, removing two tokens from P2 and adding one token to P3. Now T1 and T3 are enabled. Continuing until no more transitions are enabled, you should see
that only one token will be left in P2 after 9 transition firings. (Note that if T1 had fired instead of T2 when both were enabled, this result would have been the same after 9 firings.)

In this problem you will be presented with descriptions of one or more Petri nets. For each you are to simulate some specified number of transition firings, NF,
and then report the number of tokens remaining in the places. If the net becomes dead before NF transition firings, you are to report that fact as well.

Input

Each Petri net description will first contain an integer NP ( 0
NP < 100) followed by NP integers
specifying the number of tokens initially in each of the places numbered 1, 2,…, NP.
Next there will appear an integer NT ( 0
NT < 100) specifying the number of transitions. Then, for each transition (in increasing numerical order 1,
2,…, NT) there will appear a list of integers terminated by zero.

The negative numbers in the list will represent the input places, so the number – n indicates there is an input place at n.
The positive numbers in the list will indicate the output places, so the number pindicates an output place at p.
There will be at least one input place and at least one output place for each transition. Finally, after the description of all NT transitions, there will appear an integer
indicating the maximum number of firings you are to simulate, NF. The input will contain one or more Petri net descriptions followed by a zero.

Output

For each Petri net description in the input display three lines of output. On the first line indicate the number of the input case (numbered sequentially starting with 1) and whether or not NF transitions
were able to fire. If so, indicate the net is still live after NF firings. Otherwise indicate
the net is dead, and the number of firings which were completed. In either case, on the second line give the identities of the places which contain one or more tokens after the simulation, and the number of tokens each such place contains. This list should
be in ascending order. The third line of output for each set should be blank.

The input data will be selected to guarantee the uniqueness of the correct output displays.

Sample Input

21 02-1 2 0-2 1 010033 0 03-1 2 0-2 -2 3 0-3 1 010031 0 03-1 2 3 0-2 1 0-3 1 010

Sample Output

Case 1: still live after 100 transitionsPlaces with tokens: 1 (1)Case 2: dead after 9 transitionsPlaces with tokens: 2 (1)Case 3: still live after 1 transitionsPlaces with tokens: 2 (1) 3 (1)

模拟题,题目有点长,不过弄懂了不是很难,有点不想做模拟题,模拟题都问题太长,喜欢算法设计的题目。。。

AC代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cctype>#include <cstring>#include <string>#include <sstream>#include <vector>#include <set>#include <map>#include <algorithm>#include <stack>#include <queue>#include <bitset>#include <cassert>#include <cmath>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];// ip、op是参与变迁的place的编号int main(){int kase = 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\n", ++kase, lim);}else {printf("Case %d: dead after %d transitions\n", ++kase, cnt);}printf("Places with tokens:");for (int i = 1; i <= p; ++i) {if (np[i]) {printf(" %d (%d)", i, np[i]);}}printf("\n\n");}return 0;}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,953
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,478
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,290
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,107
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,739
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,773