题目链接
分析:
一道模拟题混入了图的题目当中,定义一个结构体数组,里面有输入,输出数组,存储一次交易的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;
}