习题6-7 Petri网模拟(Petri Net Simulation, ACM/ICPC World Finals 1998, UVa804)

0. 只是一道简答的模拟,但是我还是有点障碍。。。菜啊。。多思考。。
1. 结构体中有map时,不能用memset来初始化。
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define LL long long
#define maxn 105
#define maxm 200005
#define mod 1000000007
#define INF 1000000007
#define eps 1e-5
#define PI 3.1415926535898
#define N 26
using namespace std;
//-------------------------CHC------------------------------//
int NP, NT;
int p[maxn];
struct T {
	map pre, next;
}t[maxn];

int main() {
	int kase = 1;
	while (scanf("%d", &NP) && NP) {
		for (int i = 1; i <= NP; ++i) scanf("%d", &p[i]);
		scanf("%d", &NT);
		for (int i = 1; i <= NT; ++i) t[i].next.clear(), t[i].pre.clear();
		int x;
		for (int i = 1; i <= NT; ++i) {
			while (scanf("%d", &x) && x) {
				if (x < 0) t[i].pre[-x]++;
				else t[i].next[x]++;
			}
		}
		int trans;
		scanf("%d", &trans);
		int cnt = 0;
		bool ok = true;
		while (ok) {
			ok = false;
			for (int i = 1; i <= NT; ++i) {
				bool can = true;
				for (auto j : t[i].pre)
					if (p[j.first] < j.second) can = false;
				if (!can) continue;
				for (auto j : t[i].pre)
					p[j.first] -= j.second;
				for (auto j : t[i].next)
					p[j.first] += j.second;
				cnt++, ok = true;
				if (cnt >= trans) { ok = false; break; }
			}
		}
		if (cnt >= trans) 
			printf("Case %d: still live after %d transitions\n", kase++, trans);
		else 
			printf("Case %d: dead after %d transitions\n", kase++, cnt);
		printf("Places with tokens:");
		for (int i = 1; i <= NP; ++i)
			if (p[i]) printf(" %d (%d)", i, p[i]);
		puts("\n");
	}
	return 0;
}

你可能感兴趣的:(紫书,第六章,习题)