用链表写的,这样比较快,但只有40分
#include <iostream>
using namespace std;
typedef long long ll;
ll ord_t[410];
ll ord[22][22];//每个工件的工序
ll time_cop[22][22];//每个工序的时间
ll cnt[22][2];//记录每一个工件现在安排到第几个,以及时间
struct node {
ll start ;
ll end;
node* next = NULL;
node (int x = 0, int y = 0) :start(x), end(y) {}
};
node Machine[22];//机器链表
int main() {
//输入
int mch, cop;//机器数,工件数
cin >> mch >> cop;
for (int i = 0;i < mch * cop;i++) cin >> ord_t[i];//总的安排顺序n*m
for (int i = 1;i <= cop;i++) {
for (int j = 0;j < mch;j++) cin >> ord[i][j];
}
for (int i = 1;i <= cop;i++) {
for (int j = 0;j < mch;j++) cin >> time_cop[i][j];
}
//安排
for (int i = 0;i < mch * cop;i++) {
ll next_time;//该工件下一道工序可以开始的时间
if (Machine[ord[ord_t[i]][cnt[ord_t[i]][0]]].next == NULL) {//机器还未开始工作
node* temp = (node*)malloc(sizeof(node));
temp->start = cnt[ord_t[i]][1];
temp->end = cnt[ord_t[i]][1]+time_cop[ord_t[i]][cnt[ord_t[i]][0]];
temp->next = NULL;
next_time = cnt[ord_t[i]][1]+time_cop[ord_t[i]][cnt[ord_t[i]][0]];
Machine[ord[ord_t[i]][cnt[ord_t[i]][0]]].next = temp;
}
else {
node* temp = &Machine[ord[ord_t[i]][cnt[ord_t[i]][0]]];//temp=首节点(0,0)
int ok = 0;//是否要加在链表最后
while (temp->next != NULL) {
if (((temp->end==temp->start&& cnt[ord_t[i]][1] + time_cop[ord_t[i]][cnt[ord_t[i]][0]]<=temp->next->start)||temp->end>=cnt[ord_t[i]][1]) &&temp->next->start - temp->end >= time_cop[ord_t[i]][cnt[ord_t[i]][0]]) {
if (temp->end == temp->start) { //插在链表头到第一个节点之间
node* new_node = (node*)malloc(sizeof(node));
new_node->start = cnt[ord_t[i]][1];
new_node->end = cnt[ord_t[i]][1] + time_cop[ord_t[i]][cnt[ord_t[i]][0]];
new_node->next = temp->next;
temp->next = new_node;
next_time = temp->end + time_cop[ord_t[i]][cnt[ord_t[i]][0]];
ok = 1;
break;
}
else {
node* new_node = (node*)malloc(sizeof(node));
new_node->start = temp->end;
new_node->end = temp->end + time_cop[ord_t[i]][cnt[ord_t[i]][0]];
new_node->next = temp->next;
temp->next = new_node;
next_time = temp->end + time_cop[ord_t[i]][cnt[ord_t[i]][0]];
ok = 1;
break;
}
}
else temp = temp->next;
}
//temp已经指向最后一个节点
if (ok == 0) {//加在最后
if (temp->end >= cnt[ord_t[i]][1]) {
node* new_node = (node*)malloc(sizeof(node));
new_node->start = temp->end;
new_node->end = temp->end + time_cop[ord_t[i]][cnt[ord_t[i]][0]];
new_node->next = NULL;
next_time = temp->end + time_cop[ord_t[i]][cnt[ord_t[i]][0]];
temp->next = new_node;
}
else {
node* new_node = (node*)malloc(sizeof(node));
new_node->start = cnt[ord_t[i]][1];
new_node->end = cnt[ord_t[i]][1] + time_cop[ord_t[i]][cnt[ord_t[i]][0]];
new_node->next = NULL;
next_time = cnt[ord_t[i]][1] + time_cop[ord_t[i]][cnt[ord_t[i]][0]];
temp->next = new_node;
}
}
}
cnt[ord_t[i]][0]++;//完成一道工序,该工具的工序数+1
cnt[ord_t[i]][1] = next_time;//本工序的完成时间,下一道工序在此之后
}
ll max_time = 0;
//求所有机器最后停止工作的时间
for (int i = 1;i <= mch;i++) {
node *temp = Machine[i].next;//第一个元素
while (temp->next != NULL) temp = temp->next;
cout << temp->end<<endl;
if (max_time < temp->end) {
max_time = temp->end;
}
}
cout << max_time;
}