Petri Net Simulation UVA - 804 (Petri Net 模拟题)

题目链接

分析:

一道模拟题混入了图的题目当中,定义一个结构体数组,里面有输入,输出数组,存储一次交易的tokens数目和目标place编号。

然后以NT展开循环模拟即可。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int n,m,rnd;
int tokens[maxn];
struct Node
{
    int no,sum;
    Node(int _no, int _sum):
        no(_no), sum(_sum) {}
};
struct Tra
{
    vector<Node> from, to;
}Tra[maxn];
int from[maxn] = {0}, to[maxn];
bool judge() {
    for(int i = 1; i <= m; i++) {
        bool flag = true;
        for(int j = 0; j < (int)Tra[i].from.size(); j++) {
            int pos1 = Tra[i].from[j].no;
            int sum = Tra[i].from[j].sum; 
            if(tokens[pos1]<sum) {
                flag = false;
                break;
            }
        }
        if(flag) {
            for(int j = 0; j < (int)Tra[i].from.size(); j++) {
                int pos1 = Tra[i].from[j].no;
                int sum = Tra[i].from[j].sum;
                tokens[pos1] -= sum;
            }
            for(int j = 0; j < (int)Tra[i].to.size(); j++) {
                int pos1 = Tra[i].to[j].no;
                int sum = Tra[i].to[j].sum; 
                tokens[pos1] += sum;
            }
            return true;
        }
    }
    return false;
}

int main()
{
    freopen("i.txt","r",stdin);
    freopen("o.txt","w",stdout);
    int Case = 1;
    while(cin >> n && n) {
        memset(tokens, 0, sizeof(tokens));
        cout << "Case " << Case++ << ": ";
        int t;
        for(int i = 1; i <= n; i++)
            cin >> tokens[i];
        cin >> m;
        for(int i = 1; i <= m; i++) {
            Tra[i].from.clear();
            Tra[i].to.clear();
        }
        for(int i = 1; i <= m; i++) {
            memset(from, 0, sizeof(from));
            memset(to, 0, sizeof(to)); 
            while(cin >> t && t) {
                if(t<0) from[-t]++;
                else to[t]++;
            }
            for(int j = 1; j <= n; j++) {
                if(from[j]) Tra[i].from.push_back(Node(j,from[j]));
                if(to[j]) Tra[i].to.push_back(Node(j,to[j]));  
            }
        }
        cin >> rnd; int i;
        for(i = 0; i < rnd; i++) {
            if(!judge()) break;
        }
        if(i==rnd) cout << "still live after " << rnd << " transitions" << endl;
        else cout << "dead after " << i << " transitions" << endl;
        cout << "Places with tokens:";
        for(i = 1; i <= n; i++) {
            if(tokens[i]) cout << " " << i << " (" << tokens[i] << ")";
        }
        cout <<"\n\n";
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值