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

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
0

Sample Output

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)

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

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;
}

Uva - 804 - Petri Net Simulation的更多相关文章

  1. [刷题]算法竞赛入门经典(第2版) 6-7/UVa804 - Petri Net Simulation

    题意:模拟Petri网的执行.虽然没听说过Petri网,但是题目描述的很清晰. 代码:(Accepted,0.210s) //UVa804 - Petri Net Simulation //Accep ...

  2. 【习题 6-7 UVA - 804】Petri Net Simulation

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟就好 [代码] /* 1.Shoud it use long long ? 2.Have you ever test sever ...

  3. UVA 10700 Camel trading 无括号的表达式 贪心

    题意:给出只包含数字和+*的表达式,你可以自己安排每一个运算的顺序,让你找出表达式可能得到的最大值和最小值. 很明显,先乘后加是最小值,先加后乘能得到最大值. 其实不是很明显... 证明下: 数字的范 ...

  4. Uva 120 - Stacks of Flapjacks(构造法)

    UVA - 120  Stacks of Flapjacks Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld &a ...

  5. UVA 10057 A mid-summer night's dream. 仲夏夜之梦 求中位数

    题意:求中位数,以及能成为中位数的数的个数,以及选择不同中位数中间的可能性. 也就是说当数组个数为奇数时,中位数就只有一个,中间那个以及中位数相等的数都能成为中位数,选择的中位数就只有一种可能:如果为 ...

  6. UVA804-Petri Net Simulation(模拟)

    Problem UVA804-Petri Net Simulation Accept:251  Submit:1975 Time Limit: 3000 mSec Problem Descriptio ...

  7. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  8. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  9. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

随机推荐

  1. Spring Boot Cache Redis缓存

    1.集成MyBatis 1.1.引入maven依赖 1.2.生成Mapper 具体可以看MyBatis Generator官网 http://www.mybatis.org/generator/run ...

  2. Flume 读取RabbitMq消息队列消息,并将消息写入kafka

    首先是关于flume的基础介绍 组件名称 功能介绍 Agent代理 使用JVM 运行Flume.每台机器运行一个agent,但是可以在一个agent中包含多个sources和sinks. Client ...

  3. webpack 4 + mockjs

    一.创建项目目录 二.添加开发依赖( html-webpack-plugin.webpack.webpack-cli.webpack-dev-server.webpack-api-mocker) 如下 ...

  4. ACM Bee

    In Africa there is a very special species of bee. Every year, the female bees of such species give b ...

  5. PHP FTP 函数

    PHP FTP 简介 FTP 函数通过文件传输协议 (FTP) 提供对文件服务器的客户端访问. FTP 函数用于打开.登录以及关闭连接,同时用于上传.下载.重命名.删除及获取文件服务器上的文件信息.不 ...

  6. JavaScript for 循环

    循环可以将代码块执行指定的次数. JavaScript 循环 如果您希望一遍又一遍地运行相同的代码,并且每次的值都不同,那么使用循环是很方便的. 我们可以这样输出数组的值: 一般写法: documen ...

  7. ThreadLocal(线程绑定)

    为保证在DAO层里的操作都在同一事务里,我们曾使用以参数的形式将Connection向下传递的方式,而ThreadLocal来创建Connection连接,避免了一直以参数的形式将Connection ...

  8. YAML 在Python中的配置应用

    环境搭建 YAML语法 语法规则 数据结构 列表数组 原子量 YAML应用 案例 load dump 总结 YAML是一个堪比XML,JSON数据格式的更加方便,简洁的,易于人眼阅读的序列化数据格式. ...

  9. VMware中的桥接模式、NAT(网络地址转换模式)、Host-only(主机模式):转自:http://blog.chinaunix.net/uid-11798538-id-3061551.html

    其中VMnet1是虚拟机Host-only模式的网络接口,VMnet8是NAT模式的网络接口,这些后面会详细介绍.在个虚拟交换机,分别是-个虚拟机交换机,而在VMware Workstation 5以 ...

  10. Picasso 完美兼容 OkHttp3.3,缓存优化两不误

    Tamic 专注移动开发!更多文章请关注http://www.jianshu.com/p/6241950f9daf csdn: http://blog.csdn.net/sk719887916/art ...