804 - Petri Net Simulation(简单魔模拟)

比较简单的,注意的问题是一个输入库可能话费多个货物进行转化,需要对输入进行特殊处理

  Problem   Verdict Lang Time Best Rank Submit Time
技术分享 | discuss804 - Petri Net Simulation  Accepted C++ 0.099 0.028 14 2 mins ago
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 105;
int np,nt;
struct Node{
    int id;
    int num;
    Node(int id = 0,int num = 0):id(id),num(num){};
};
vector<Node>in[maxn];
vector<Node>out[maxn];
int np_num[maxn],arr[100005];
bool solve(){
    for(int i = 1; i <= nt; i++){
        int ok = 1;
        for(int j = 0; j < in[i].size(); j++){
            int id = in[i][j].id,num = in[i][j].num;
            if(np_num[id] < num){
                ok = 0;
                break;
            }
        }
        if(ok){
            for(int j = 0; j < in[i].size(); j++){
                int id = in[i][j].id,num = in[i][j].num;
                np_num[id] -= num;
            }
            for(int j = 0; j < out[i].size(); j++){
                int id = out[i][j].id,num = out[i][j].num;
                np_num[id] += num;
            }
            return true;
        }
    }
    return false;
}
int main(){
    int Case = 1;
    //freopen("in.txt","r",stdin);
    while(scanf("%d",&np) && np){
        for(int i = 1; i <= np; i++)
            scanf("%d",&np_num[i]);
        scanf("%d",&nt);
        for(int i = 1; i <= nt; i++){
            int cnt = 0;
            in[i].clear(); out[i].clear();
            while(scanf("%d",&arr[cnt]) && arr[cnt])
                cnt ++;
            sort(arr,arr + cnt);
            if(cnt == 0) continue;
            int id = arr[0],num = 1;
            for(int j = 1; j < cnt; j++){
                if(arr[j] != arr[j - 1]){
                    if(id < 0)
                        in[i].push_back(Node(-id,num));
                    if(id > 0)
                        out[i].push_back(Node(id,num));
                    id = arr[j];
                    num = 1;
                }
                else num++;
            }
            if(id < 0)
                in[i].push_back(Node(-id,num));
            if(id > 0)
                out[i].push_back(Node(id,num));
        }
        int cir,i;
        scanf("%d",&cir);
        for(i = 1; i <= cir; i++){
            if(!solve())
                break;
        }
        if(i > cir)
            printf("Case %d: still live after %d transitions\n",Case++,i - 1);
        else
            printf("Case %d: dead after %d transitions\n",Case++,i - 1);
        printf("Places with tokens:");
        for(int i = 1; i <= np; i++){
            if(np_num[i])
                printf(" %d (%d)",i,np_num[i]);
        }
        puts("\n");
    }
    return 0;
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。