Petri Net Simulation UVA - 804

模拟,对于每一个transition,记录从该transition开始到相应的place所导致的place中的token数量的变化,对于某个place而言,place扮演的是输入的角色,那么如果相应的fire发生,则会导致place中的token数量减少1,反之,如果扮演的是接收transition输入的角色,那么相应的fire发生,会导致place中的token数量增加1。然后开始模拟,一共可以模拟的轮数不能超过制定的数量,同时对于每一轮的模拟,要分别分析每一个transition,如果transition所代表的操作能够发生(也就是对于该transition而言,可以得到足够的输入,同时可以产生足够的输出),那么fire数量增1,同时要更新相应的fire发生后,每个place中的token数量。每次结束后要判断当前产生的fire数量是否达到要求,如果达到要求,就可以直接进行最后的输出操作。具体实现见如下代码:

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
using namespace std;

int Np, Nt, Nf;
int place[110];
int amount[110][110];

int main(){
	int Case = 0;
	while (cin >> Np){
		memset(place,0,sizeof(place));
		memset(amount,0,sizeof(amount));
		Case++;
		if (Np == 0) break;
		for (int i = 1; i <= Np; i++) cin >> place[i];
		cin >> Nt;
		for (int i = 1; i <= Nt; i++){
			int t;
			while (cin >> t){
				if (t == 0) break;
				else if (t > 0) amount[i][t]++;
				else amount[i][-t]--;
			}
		}
		cin >> Nf;
		int round = 0;
		for (int i = 1; i <= Nf; i++){
			for (int j = 1; j <= Nt; j++){
				bool flag = true;
				for (int k = 1; k <= Np; k++){
					if (place[k] + amount[j][k] < 0){
						flag = false;
						break;
					}
				}
				if (flag){
					round++;
					for (int k = 1; k <= Np; k++) place[k] += amount[j][k];
					if (round >= Nf) break;
				}
			}
			if (round >= Nf) break;
		}
		cout << "Case " << Case << ": ";
		if (round >= Nf) cout << "still live after "<<Nf<<" transitions" << endl;
		else cout << "dead after " << round << " transitions" << endl;
		cout << "Places with tokens:";
		for (int i = 1; i <= Np; i++){
			if (place[i] > 0){
				cout << " " << i << " (" << place[i] << ")";
			}
		}
		cout << endl << endl;
	}
	system("pause");
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值