804 - Petri Net Simulation

Input: petri.in APetri net is a computational model used to illustrate concurrent activity. EachPetri net contains some number of places (represented by circles), transitions(represented by black rectangles), and directed edges used to connect places totransitions, and transitions to places. Each place can hold zero or more tokens(represented by black dots). Here are two examples:

In the first Petrinet above, there are two places (P1 and P2) and two transitions (T1 and T2). P1initially has one token; P2 has none. P1 is an input place for transition T1,and P2 is an output place for T1. In the second example there are three placesand three transitions, with three tokens in P1. T2 has two input places, bothof which are P2.

Operation of a Petri Net 

Each transition ina Petri net is either enabled or disabled. A transition is enabled if there isat least one token in each of its input places. Any transition can firewhenever it is enabled. If multiple transitions are enabled, any one of themmay fire. When a transition fires, one token is removed from each of the inputplaces, and one token is added to each of the output places; this iseffectively done atomically, as one action. When there are no enabledtransitions, a Petri net is said to be dead.

In the top exampleonly T1 is enabled. When it fires one token is removed from P1, and one tokenis added to P2. Then T2 is enabled. When it fires one token is removed from P2,and one token is added to P1. Clearly this Petri net will repeat this cycleforever.

The bottom exampleis more interesting. T1 is enabled and fires, effectively moving a token to P2.At this point T1 is still the only enabled transition (T2 requires that P2 havetwo tokens before it is enabled). T1 fires again, leaving one token in P1 andtwo tokens in P2. Now both T1 and T2 are enabled. Assume T2 fires, removing twotokens from P2 and adding one token to P3. Now T1 and T3 are enabled.Continuing until no more transitions are enabled, you should see that only onetoken will be left in P2 after 9 transition firings. (Note that if T1 had firedinstead of T2 when both were enabled, this result would have been the sameafter 9 firings.)

In this problemyou will be presented with descriptions of one or more Petri nets. For each youare to simulate some specified number of transition firings, NF,and then report the number of tokens remaining in the places. If the netbecomes dead before NF transition firings, you are toreport that fact as well.

Input 

Each Petri netdescription will first contain an integer NP ( 0< NP < 100) followed by NPintegersspecifying the number of tokens initially in each of the placesnumbered 1, 2,..., NP. Next there will appear aninteger NT ( 0 < NT <100) specifying the number of transitions. Then, for each transition (in increasingnumerical order 1, 2,..., NT) there will appear a listof integers terminated by zero.

The negativenumbers in the list will represent the input places, so the number - n indicatesthere is an input place at n. The positive numbers in thelist will indicate the output places, so the number p indicatesan output place at p. There will be at least one input placeand at least one output place for each transition. Finally, after thedescription of allNT transitions, there will appear aninteger indicating the maximum number of firings you are to simulate, NF.The input will contain one or more Petri net descriptions followed by a zero.

Output 

For each Petri netdescription in the input display three lines of output. On the first lineindicate the number of the input case (numbered sequentially starting with 1)and whether or not NF transitions were able to fire. Ifso, indicate the net is still live after NFfirings.Otherwise indicate the net is dead, and the number of firings which werecompleted. In either case, on the second line give the identities of the placeswhich contain one or more tokens after the simulation, and the number of tokenseach such place contains. This list should be in ascending order. The thirdline of output for each set should be blank.

The input datawill be selected to guarantee the uniqueness of the correct output displays.

Sample Input 

2

1 0

2

-1 2 0

-2 1 0

100

3

3 0 0

3

-1 2 0

-2 -2 3 0

-3 1 0

100

3

1 0 0

3

-1 2 3 0

-2 1 0

-3 1 0

1

0

Sample Output 

Case 1: still liveafter 100 transitions

Places withtokens: 1 (1)

 

Case 2: dead after9 transitions

Places withtokens: 2 (1)

 

Case 3: still liveafter 1 transitions

Places withtokens: 2 (1) 3 (1)

t'>x3� #x<87��8>

In the next linethe distribution of 0's and 1's over the terminal nodes is given. There will beexactly 2n characters (each of which can be 0 or 1),followed by the new-line character. The characters are given in the order inwhich they appear in the S-tree, the first character corresponds to theleftmost terminal node of the S-tree, the last one to its rightmost terminalnode.

The next linecontains a single integer m, the number of VVAs, followed by m linesdescribing them. Each of the m lines contains exactly n characters(each of which can be 0 or 1), followed by a new-line character. Regardless ofthe variable ordering of the S-tree, the first character always describes thevalue of x1, the second character describes the valueof x2, and so on. So, the line

110

corresponds to theVVA ( x1 = 1, x2 =1, x3 = 0).

The input isterminated by a test case starting with n = 0. This test caseshould not be processed.

Output 

For each S-tree,output the line ``S-Tree #j:", where j is thenumber of the S-tree. Then print a line that contains the value of   for each of the given m VVAs,where f is the function defined by the S-tree.

Output a blankline after each test case.

Sample Input 

3

x1 x2 x3

00000111

4

000

010

111

110

3

x3 x1 x2

00010011

4

000

010

111

110

0

Sample Output 

S-Tree#1:

0011

 

S-Tree #2:

0011

 

代码:

#include<iostream>

#include<cstring>

#include<cmath>

using namespacestd;

 

int order[10];

char tree[1000];

 

int main()

{

    int n;

    int kase=1;

   

    while(cin>>n&&n)

    {

        memset(order,0,sizeof(order));

        memset(tree,0,sizeof(tree));

       

        string s;

        for(int i=1;i<=n;i++)

        {

            cin>>s;

            order[i]=s[1]-'0';

        }

       

        cin>>s;

        int first=pow(2,n);

        int last=pow(2,n+1)-1;

        for(int i=0;first<=last;first++,i++)

        {

            tree[first]=s[i];

        }

       

        int m;

        cin>>m;

        cout<<"S-Tree#"<<kase++<<":\n";

       

        while(m--)

        {

            cin>>s;

            int k=1;

            for(int i=1;i<=n;i++)

            {

                if(s[order[i]-1]=='0')

                {

                    k=k*2;

                }

                else

                {

                    k=k*2+1;

                }

            }

            cout<<tree[k];

        }

        cout<<"\n\n";

    }

   

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值