uva 804 Petri Net 模拟
2015-11-15 15:11
471 查看
原题
普通的模拟题, 题目虽然比较长但流程还是比较简单的, 所以主要还是几个细节的问题处理好就行了。
注意如果所给数据只能进行N轮循环, 而要求的循环数NF正好等于N时,执行完N轮退出时的状态仍然是alive,虽然下一步已经没有操作可做了。
奇怪的是一开始不知道为啥超时了,然后似乎没改进什么复杂度只改了几个细节再交上去就AC了。。。
以下为AC代码:
普通的模拟题, 题目虽然比较长但流程还是比较简单的, 所以主要还是几个细节的问题处理好就行了。
注意如果所给数据只能进行N轮循环, 而要求的循环数NF正好等于N时,执行完N轮退出时的状态仍然是alive,虽然下一步已经没有操作可做了。
奇怪的是一开始不知道为啥超时了,然后似乎没改进什么复杂度只改了几个细节再交上去就AC了。。。
以下为AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <string> #include <vector> #include <set> #include <stack> #include <queue> #include <deque> #include <map> #include <list> #include <cassert> #include <iomanip> #pragma warning(disable:4996) //关掉4996警告 /* Uva 804 佩特里网络模拟 */ using namespace std; struct Transit { vector<int> from; vector<int> to; }; vector<int> Places(1024); vector<Transit> Transits(1024); int NT, NP, NF, Case = 0; inline bool isAlive(int n) { bool isEnable = true; map<int, int> need; for ( int j = 0; j < Transits .from.size( ); j++ ) { need[Transits .from[j]] += 1; if ( Places[Transits .from[j]] < need[Transits .from[j]] ) { isEnable = false; break; } } return isEnable; } int Simulate(int times, bool & alive) { int tmp = times; while ( times-- ) { int i = 0; for ( i = 1; i <= NT; i++ ) { if ( isAlive(i) ) { for ( int j = 0; j < Transits[i].from.size( ); j++ ) { Places[Transits[i].from[j]]--; } for ( int j = 0; j < Transits[i].to.size( ); j++ ) { Places[Transits[i].to[j]]++; } break; // 完成一次Trans } } if ( i > NT ) { alive = false; break; // Dead } } return (tmp - (times + 1)); } int main( ) { //freopen("input.txt", "r", stdin); //freopen("VSoutput.txt", "w", stdout); while ( cin >> NP && NP > 0 ) { Places.clear( ); Transits.clear( ); Places.resize(1024); for ( int i = 1, tmp; i <= NP; i++ ) { cin >> tmp; Places[i] = tmp; } cin >> NT; Transits.resize(1024); for ( int i = 1, tmp; i <= NT; i++ ) { while ( cin >> tmp && tmp != 0 ) { if ( tmp < 0 ) Transits[i].from.push_back(-tmp); else Transits[i].to.push_back(tmp); } } cin >> NF; bool alive = true; int turns = Simulate(NF, alive); printf("Case %d: %s after %d transitions\n", ++Case, (alive ? "still live" : "dead"), turns); printf("Places with tokens:"); for ( int i = 1; i <= NP; i++ ) { if ( Places[i] ) { printf(" %d (%d)", i, Places[i]); } } cout << endl << endl; } //system("pause"); return 0; }
相关文章推荐
- (PHP)正则表达式-贪婪匹配与懒惰匹配的区别
- 【JavaScript】——BOM+demo
- 概率分布函数经过基本运算函数的结果
- javascript快速入门第一章基础知识(上)
- autolayout更改约束后操作
- html中<a>标签中锚点的用法
- UI之uiNavigationCtroller and navigationBar UIappearance navigationItem
- 移动终端基带芯片基本架构
- gdb 调试openmp多线程程序 示例介绍
- 理解递归操作
- jquery获取点击事件对象
- 一个获取a标签传值的函数
- Activity生命周期
- LeetCode Product of Array Except Self 数组
- Windows7 64bit VS2013 Caffe train MNIST操作步骤
- Windows7 64bit VS2013 Caffe train MNIST操作步骤
- vc中内存对齐
- 汇编里寄存器的作用
- ERROR 001362:Failed to extract manifest from uploaded service definition.无法从上传的服务定义中提取清单。
- 安装win10体验