Problem UVA804-Petri Net Simulation

Accept:251  Submit:1975

Time Limit: 3000 mSec

Problem Description

 Input

Each Petri net description will first contain an integer NP (0 < NP < 100) followed by NP integers specifying the number of tokens initially in each of the places numbered 1,2,...,NP. Next there will appear an integer NT (0 < NT < 100) specifying the number of transitions. Then, for each transition (in increasing numerical order 1,2,...,NT) there will appear a list of integers terminated by zero. The negative numbers in the list will represent the input places, so the number −n indicates there is an input place at n. The positive numbers in the list will indicate the output places, so the number p indicates an output place at p. There will be at least one input place and at least one output place for each transition. Finally, after the description of all NT transitions, there will appear an integer 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 net description in the input display three lines of output. On the first line indicate the number of the input case (numbered sequentially starting with 1) and whether or not NF transitions were able to fire. If so, indicate the net is still live after NF firings. Otherwise indicate the net is dead, and the number of firings which were completed. In either case, on the second line give the identities of the places which contain one or more tokens after the simulation, and the number of tokens each such place contains. This list should be in ascending order. The third line of output for each set should be blank. The input data will 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
0
 

 Sample Ouput

Case 1: still live after 100 transitions

Places with tokens: 1 (1)
Case 2: dead after 9 transitions

Places with tokens: 2 (1)

题解:这个题就是个模拟,题意稍有一些难懂(英文比紫书的中文好懂......)

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
using namespace std; const int maxn = +; struct Place{
int token;
}place[maxn]; struct Point{
int pos,sum;
Point(int pos = ,int sum = ) :
pos(pos),sum(sum) {}
}; struct Tra{
vector<Point> input,output;
}Tra[maxn]; int n,m,iCase = ;
int in[maxn],out[maxn]; bool solve(){
for(int i = ;i <= m;i++){
bool ok = true;
for(int j = ;j < (int)Tra[i].input.size();j++){
int pos = Tra[i].input[j].pos;
int sum = Tra[i].input[j].sum;
if(place[pos].token < sum){
ok = false;
break;
}
}
if(ok){
for(int k = ;k < (int)Tra[i].input.size();k++){
int pos = Tra[i].input[k].pos;
int sum = Tra[i].input[k].sum;
place[pos].token -= sum;
}
for(int k = ;k < (int)Tra[i].output.size();k++){
int pos = Tra[i].output[k].pos;
int sum = Tra[i].output[k].sum;
place[pos].token += sum;
}
return true;
}
}
return false;
} int main()
{
//freopen("input.txt","r",stdin);
while(~scanf("%d",&n) && n){
for(int i = ;i <= n;i++){
scanf("%d",&place[i].token);
}
scanf("%d",&m);
for(int i = ;i <= m;i++){
Tra[i].input.clear();
Tra[i].output.clear();
}
int x;
for(int i = ;i <= m;i++){
memset(in,,sizeof(in));
memset(out,,sizeof(out));
while(scanf("%d",&x) && x){
if(x < ) in[-x]++;
else out[x]++;
}
for(int t = ;t <= n;t++){
if(in[t]) Tra[i].input.push_back(Point(t,in[t]));
if(out[t]) Tra[i].output.push_back(Point(t,out[t]));
}
}
int round;
scanf("%d",&round);
int tt;
for(tt = ;tt <= round;tt++){
if(!solve()) break;
}
printf("Case %d: ",iCase++);
if(tt > round) printf("still live after %d transitions\n",round);
else printf("dead after %d transitions\n",tt-);
printf("Places with tokens:");
for(int i = ;i <= n;i++){
if(place[i].token){
printf(" %d (%d)",i,place[i].token);
}
}
printf("\n\n");
}
return ;
}

UVA804-Petri Net Simulation(模拟)的更多相关文章

  1. [刷题]算法竞赛入门经典(第2版) 6-7/UVa804 - Petri Net Simulation

    题意:模拟Petri网的执行.虽然没听说过Petri网,但是题目描述的很清晰. 代码:(Accepted,0.210s) //UVa804 - Petri Net Simulation //Accep ...

  2. Uva - 804 - Petri Net Simulation

    Input: petri.in A Petri net is a computational model used to illustrate concurrent activity. Each Pe ...

  3. 【习题 6-7 UVA - 804】Petri Net Simulation

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟就好 [代码] /* 1.Shoud it use long long ? 2.Have you ever test sever ...

  4. Leetcode874.Walking Robot Simulation模拟行走的机器人

    机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方.该机器人可以接收以下三种类型的命令: -2:向左转 90 度 -1:向右转 90 度 1 <= x <= 9:向 ...

  5. LightOJ Beginners Problems 部分题解

    相关代码请戳 https://coding.net/u/tiny656/p/LightOJ/git 1006 Hex-a-bonacci. 用数组模拟记录结果,注意取模 1008 Fibsieve's ...

  6. 蒙特卡洛树搜索算法(UCT): 一个程序猿进化的故事

    前言: 本文是根据的文章Introduction to Monte Carlo Tree Search by Jeff Bradberry所写. Jeff Bradberry还提供了一整套的例子,用p ...

  7. NeHe OpenGL教程 第三十九课:物理模拟

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  8. swat主流域文件(file.cio)参数详解——引自http://blog.sciencenet.cn/blog-922140-710636.html

    % file.clo,即主流域文件用于文件管理,包括与模型选项.气候输入.数据库和输出控制相关的信息. Master Watershed File: file.cio Project Descript ...

  9. QM5_Didstribution

    Basic Concepts Probability distribution Discrete distribution (离散分布) The distribution of the discret ...

  10. Cisco Packet Tracer中通过集线器组网

    Cisco Packet Tracer中可以通过集线器将多台电脑完成通信. Cisco Packet Tracer 6.2.0 一.添加三台电脑设备 1.按照下图1.2步骤操作,2步骤执行三次,拖拽P ...

随机推荐

  1. Apollo 2 如何支持 @Value 注解自动更新

    前言 Apollo 在 v0.10.0 版本后,支持自动更新.v0.10.0之前的版本在配置变化后不会重新注入,需要重启才会更新. 也就是说,如果一个属性加入了 @Value 注解,并且这个配置在配置 ...

  2. Linux中rm命令详解

    linux下rm命令使用详解---linux删除文件或目录命令 用户可以用rm命令删除不需要的文件.该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除 ...

  3. 弱引用(WeakReference)

    在应用程序代码内实例化一个类或结构时,只要有代码引用它,就会形成强引用.这意味着垃圾回收器不会清理这样的对象使用的内存.但是如果当这个对象很大,并且不经常访问时,此时可以创建对象的弱引用,弱引用允许创 ...

  4. JavaSE Map集合

    Map集合 在Map集合中保存的数据为一组数据,其中:一个数据为key,另外一个数据为value.而key和value具备对应的关系,在集合中它们属于一组(一对)数据.而每个key只能对应唯一的一个v ...

  5. elasticsearch6.7 01.入门指南(1)

    Elasticsearch 是一个高度可扩展且开源的全文检索和分析引擎.它可以让您快速.近实时地存储.检索以及分析海量数据.它通常用作那些具有复杂搜索功能和需求的应用的底层引擎或者技术. 下面是 El ...

  6. Vsftpd+Tengine+SpringMVC实现上传图片

    第三部分:SpringMVC实现上传 1.1 思路 (1)使用SpringMVC上传组件,从页面表单接收图片 (2)使用vsftpd组件,将图片上传到Linux服务器 a.服务端:在Linux上安装f ...

  7. windows使用笔记-google-chrome下载地址

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! google-chrome下载地址:https://www.google.cn/intl/zh-CN/chrome/

  8. 畅通工程续(HDU 1874)附上超详细源代码

    Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行 ...

  9. WEB前端面试2014阿里旺旺

    NO1.下图绿色区域的宽度为100%,其中有三个矩形,第一个矩形的宽度是200px,第二个和第三个矩形的宽度相等.请使用css3中的功能实现它们的布局. 已知HTML结构是: <div clas ...

  10. python的四大函数讲解

    Python的四类函数: 1.普通函数 2.默认函数 3.关键字函数 4.收集参数 1.普通函数 a.定义的时候直接定义变量名 b.调用的时候直接把变量或者值放入指定位置 def 函数名(参数1,参数 ...