例题6-1 并行程序模拟(Concurrency Simulator, ACM/ICPC World Finals 1991, UVa210)

#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#define CLOSE() ios::sync_with_stdio(false)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
const int maxn = 100005;
using LL = long long;
using UI = unsigned int;
using namespace std;
//------------------------------------------------------------------------------------------//
//STL实现。
vector<deque<string>> prog;

int main() {
#ifdef _DEBUG
	IN(); OUT();
#endif
	int T;
	scanf("%d", &T);
	while (T--) {
		int n, time[5], qutm;
		scanf("%d", &n);
		for (int i = 0; i < 5; ++i) scanf("%d", &time[i]);
		string s;
		scanf("%d", &qutm); getline(cin, s);
		prog.clear();
		deque<int> ready;
		for (int i = 0; i < n; ++i) {
			ready.push_back(i);
			while (getline(cin, s)) {
				prog.push_back(deque<string>());
				prog[i].push_back(s);
				//cout << s << endl;
				if (s == "end") break;
			}
			//cout << endl;
		}
		queue<int> blocked;
		map<char, int> vari;
		bool lock = false;
		while (!ready.empty()) {
			int nowprog = ready.front(); ready.pop_front(); ready.push_back(nowprog);
			//printf("now = %d\n", nowprog+1);
			int t = qutm;
			while (t > 0) {
				string inst = prog[nowprog].front(); prog[nowprog].pop_front();
				int p = inst.find('='), p2 = inst.find("print");
				if (inst == "end") {
					ready.pop_back();
					break;
				}
				else if (p != -1) {
					vari[inst[p - 2]] = atoi(inst.substr(p + 2).c_str());
					t -= time[0];
				}
				else if (p2 != -1) {
					char va = inst[p2 + 6];
					printf("%d: %d\n", nowprog+1, vari[va]);
					t -= time[1];
				}
				else if (inst == "lock") {
					if (!lock) {
						lock = true;
						t -= time[2];
					}
					else {
						ready.pop_back();
						blocked.push(nowprog);
						prog[nowprog].push_front(string("lock"));
						break;//执行完后记得break
					}
				}
				else {
					t -= time[3];
					lock = false;
					if (!blocked.empty()) {
						ready.push_front(blocked.front());
						blocked.pop();
					}
				}
			}
		}
		if (T) puts("");
	}
	return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#define CLOSE() ios::sync_with_stdio(false)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
const int maxn = 100005;
using LL = long long;
using UI = unsigned int;
using namespace std;
//------------------------------------------------------------------------------------------//
vector<vector<string>> prog;
vector<pair<int, int>> rf;
int readyq[maxn], rrear, rfront;
int blocked[maxn], brear, bfront;

//自行实现双端队列

int main() {
#ifdef _DEBUG  
	IN(); OUT();
#endif 
	int T;
	scanf("%d", &T);
	while (T--) {
		int n, time[5], qutm;
		scanf("%d", &n);
		for (int i = 0; i < 5; ++i) scanf("%d", &time[i]);
		scanf("%d", &qutm); getchar();
		prog.clear(); rf.clear();
		CLEAR(readyq, 0); rrear = rfront = 0;
		for (int i = 0; i < n; ++i) {
			readyq[rfront++] = i;
			string s;
			while (getline(cin, s)) {
				prog.push_back(vector<string>());
				rf.push_back(make_pair(0, 0));
				prog[i].push_back(s);
				//cout << s << endl;
				if (s == "end") break;
			}
			//cout << endl;
		}
		CLEAR(blocked, 0); brear = bfront = 0;
		map<char, int> vari;
		bool lock = false;
		while (rrear != rfront) {
			int nowprog = readyq[rrear++]; readyq[rfront++] = nowprog;
			//printf("now = %d\n", nowprog+1);  
			int t = qutm;
			while (t > 0) {
				string inst = prog[nowprog][rf[nowprog].first++];
				int p = inst.find('='), p2 = inst.find("print");
				if (inst == "end") {
					rfront--;
					break;
				}
				else if (p != -1) {
					vari[inst[p - 2]] = atoi(inst.substr(p + 2).c_str());
					t -= time[0];
				}
				else if (p2 != -1) {
					char va = inst[p2 + 6];
					printf("%d: %d\n", nowprog + 1, vari[va]);
					t -= time[1];
				}
				else if (inst == "lock") {
					if (!lock) {
						lock = true;
						t -= time[2];
					}
					else {
						rfront--;
						blocked[bfront++] = nowprog;
						rf[nowprog].first--;
						break;  
					}
				}
				else {
					t -= time[3];
					lock = false;
					if (brear != bfront) {
						readyq[--rrear] = blocked[brear++];
					}
				}
			}
		}
		if (T) puts("");
	}
	return 0;
}



版权声明:本文为chcnsn原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。