UVA804 Petri Net Simulation(模拟)

本题vjudge链接

  • 每个T变迁都有限制,必须是指定的P库有足够的token才行(注意,T指定的P库不一定只有一个),直接模拟就好了
#include <bits/stdc++.h>

using namespace std;

int p[110], np, nt, kase = 0;

struct T {
    int in[110], out[110];
    T() {
        memset(in, 0, sizeof in);
        memset(out, 0, sizeof out);
    }
};

int main() {
#ifndef ONLINE_JUDGE
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#endif
    for (; scanf("%d", &np), np;) {
        for (int i = 1; i <= np; i++) {
            scanf("%d", p + i);
        }
        scanf("%d", &nt);
        int x;
        T ts[110];
        for (int i = 1; i <= nt; i++) {
            for (; scanf("%d", &x), x;) {
                if (x < 0) ts[i].in[-x]++;
                else ts[i].out[x]++;
            }
        }
        int nf, n;
        scanf("%d", &nf);
        bool ok = true;
        for (n = 0; n < nf && ok;) {
            for (int i = 1; i <= nt; i++) {
                ok = true;
                for (int j = 1; j <= np; j++) {
                    if (p[j] < ts[i].in[j]) {
                        ok = false;
                        break;
                    }
                }
                if (ok) {
                    for (int j = 1; j <= np; j++) {
                        p[j] -= ts[i].in[j];
                        p[j] += ts[i].out[j];
                    }
                    n++;
                    break;
                }
            }
        }
        vector<int> ans;
        for (int i = 1; i <= np; i++) {
            if (p[i] != 0) {
                ans.push_back(i);
            }
        }
        if (n < nf) printf("Case %d: dead after %d transitions\n", ++kase, n);
        else printf("Case %d: still live after %d transitions\n", ++kase, nf);
        printf("Places with tokens:");
        for (auto i : ans) {
            printf(" %d (%d)", i, p[i]);
        }
        puts("\n");
        memset(p, 0, sizeof p);
    }

    return 0;
}

原地址

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值