习题6-7 Petri网模拟 (Petri Net Simulation, ACM/ICPC World Finals 1998, UVa804)
Posted As_zyh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了习题6-7 Petri网模拟 (Petri Net Simulation, ACM/ICPC World Finals 1998, UVa804)相关的知识,希望对你有一定的参考价值。
理解题意后不难
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
int NP, NT, n;
int tokens[110];
char s[1000];
vector<int> from[110];
vector<int> to[110];
int main()
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int q = 0;
while(scanf("%d", &NP) == 1 && NP)
q++;
memset(tokens, 0, sizeof(tokens));
for(int i = 0; i < 110; i++)
from[i].clear();
to[i].clear();
for(int i = 0; i < NP; i++) scanf("%d", &tokens[i+1]);
scanf("%d", &NT);
for(int i = 0; i < NT; i++)
int x;
while(scanf("%d", &x) == 1 && x)
if(x < 0) from[i].push_back(x*(-1));
else to[i].push_back(x);
scanf("%d", &n);
int cnt = 0;
while(cnt < n)
int flag;
for(int i = 0; i < NT; i++)
//变迁 T i+1
int x;
flag = 0; //变迁是否能完整执行
for(int j = 0; j < from[i].size(); j++)
x = from[i][j];
if(tokens[x] > 0) tokens[x]--;
else
//回滚
for(int k = j-1; k >= 0; k--)
x = from[i][k];
tokens[x]++;
flag = 0;
break;
flag = 1;
if(!flag) continue; //此变迁不能执行,尝试下一个变迁
for(int j = 0; j < to[i].size(); j++)
x = to[i][j];
tokens[x]++;
if(flag)
cnt++;break;
if(!flag) break; //没有一个变迁可以执行
if(cnt == n) printf("Case %d: still live after %d transitions\\n", q, cnt);
else printf("Case %d: dead after %d transitions\\n", q, cnt);
printf("Places with tokens:");
for(int i = 1; i < 110; i++)
if(tokens[i] > 0) printf(" %d (%d)", i, tokens[i]);
printf("\\n\\n");
return 0;
以上是关于习题6-7 Petri网模拟 (Petri Net Simulation, ACM/ICPC World Finals 1998, UVa804)的主要内容,如果未能解决你的问题,请参考以下文章