过程稍微复杂,解析见注释
#include<iostream>
#include<vector>
#include<string>
#include<type_traits>
#include<sstream>
#include<tuple>
#include<bitset>
#include<regex>
#include<set>
#include<stack>
#include<queue>
#include<map>
using namespace std;
typedef struct{
string id_p;//工作人员编号
int lst_w_t;//上一份工作的开始时间
int work_time;//目前的工作时间
int area_amount;//可以完成的工作类型总数
int do_id;//记录正在做的工作对应的下标
vector<string> area;//可做工作id
}person;//服务工作人员
typedef struct{
string id_w;//工作的种类
int amount_w;//该类工作的总数
int start_time;//下一次开始请求到来的时间
int serve_t;//服务时间
int wait_t;//等待时间
int wait_queue;//等待队列的长度
int finish;//已经完成的数量
}req;//请求
bool compare(person a,person b){
if (a.lst_w_t != b.lst_w_t) return a.lst_w_t < b.lst_w_t;
return a.id_p < b.id_p;
}
int main(){
int m, n;
int i_case = 0;
while (cin >> m){
i_case++;
if (m == 0) break;
map<string, int> id_w2ind;//建立工作种类到下标的映射
int total_finish = m;
vector<req> work;
for (int i = 0; i < m; i++){
req temp;
cin >> temp.id_w >> temp.amount_w >> temp.start_time >> temp.serve_t >> temp.wait_t;
temp.wait_queue = temp.finish = 0;
work.push_back(temp);
id_w2ind[temp.id_w] = i;
}
cin >> n;
vector<person> per;
for (int i = 0; i < n; i++){
person temp;
cin >> temp.id_p >> temp.area_amount;
for (int j = 0; j < temp.area_amount; j++){
string t;
cin >> t;
temp.area.push_back(t);
}
temp.work_time = 0;
temp.lst_w_t = 0;
per.push_back(temp);
}
int current = 0;
bool flag = false;
vector<int> excute(m,0);//表示当前对应的工作是否正在执行
while (true){
for (int i = 0; i < work.size(); i++){
if (current >= work[i].start_time&&work[i].finish + work[i].wait_queue+excute[i] < work[i].amount_w){
work[i].start_time += work[i].wait_t;
++work[i].wait_queue;
}
}
sort(per.begin(),per.end(),compare);
for (int i = 0; i < per.size(); i++){
if (per[i].work_time == 0){
for (int j = 0; j < per[i].area.size(); j++){
int index = id_w2ind[per[i].area[j]];
if (work[index].wait_queue > 0){
work[index].wait_queue--;
excute[index] = 1;
per[i].work_time = work[index].serve_t;
per[i].lst_w_t = current;
per[i].do_id = index;
break;
}
}
}
}
current++;
for (int i = 0; i < per.size(); i++){
per[i].work_time--;
if (per[i].work_time == 0){
int index = per[i].do_id;
work[index].finish++;
excute[index] = 0;
if (work[index].finish == work[index].amount_w) total_finish--;
if (total_finish == 0){
flag = true;
break;
}
}
per[i].work_time = max(per[i].work_time, 0);
}
if (flag) break;
}
//Scenario 1: All requests are serviced within 195 minutes.
cout <<"Scenario "<<i_case<<": All requests are serviced within "<< current<<" minutes." << endl;
}
//system("pause");
return 0;
}