#P1065. [NOIP 2006 提高组] 作业调度方案

[NOIP 2006 提高组] 作业调度方案

Description

We need to process nn jobs on mm machines. Each job has mm operations, and every operation must be completed on a specified different machine. Each operation of each job has a specified processing time.

Every operation of every job is called an action. We denote an action by j-k, where jj is a number from 11 to nn indicating the job ID, and kk is a number from 11 to mm indicating the operation index. For example, 2-4 denotes the action of the 44-th operation of job 22. In this problem, we are also given an arrangement order for all actions.

For example, when n=3n=3, m=2m=2, the sequence 1-1,1-2,2-1,3-1,3-2,2-2 is a given arrangement order, i.e., first arrange the 11-st operation of job 11, then the 22-nd operation of job 11, then the 11-st operation of job 22, and so on.

On one hand, arranging each action must satisfy the following two constraints.

  1. For the same job, each operation must start only after all its preceding operations have finished.

  2. At any time, each machine can process at most one job.

On the other hand, when arranging later actions, the working states of actions already arranged earlier cannot be changed.

Since the operations of the same job are arranged in order, providing only the job IDs in the original order still yields the same arrangement order. Thus, in the input, this arrangement order is written in a shortened form as 1 1 2 3 3 2.

Note that the “arrangement order” only requires arranging each action in the given order. It is not necessarily the actual execution order on each machine. In actual execution, an action later in the list may finish earlier than one that appears earlier.

For example, take n=3n=3, m=2m=2, with the following data (machine ID/processing time):

Job ID Operation 1 Operation 2
11 1/31/3 2/22/2
22 1/21/2 2/52/5
33 2/22/2 1/41/4

Then for the arrangement order 1 1 2 3 3 2, the two implementation plans below are both valid. However, the total times required are 1010 and 1212, respectively.

Plan 1, time 1010:

Time 1 2 3 4 5 6 7 8 9 10
Machine 1 action 1-1 2-1 3-2 Idle
Machine 2 action 3-1 Idle 1-2 2-2

Plan 2, time 1212:

Time 1 2 3 4 5 6 7 8 9 10 11 12
Machine 1 action 1-1 2-1 Idle 3-2 Idle
Machine 2 action Idle 1-2 3-1 2-2

When inserting an action into some idle interval (gap) on a machine (the final unarranged segment on a machine can also be regarded as an idle interval), it can be inserted at the front, back, or somewhere in the middle. To simplify the problem, we stipulate: under constraints (1) and (2), insert as early as possible. Moreover, if multiple idle intervals can accept the insertion, insert into the earliest one under constraints (1) and (2). Under these rules, Plan 1 above is correct, while Plan 2 is not.

Clearly, with these rules, for a given arrangement order, the implementation plan consistent with it is unique. Please compute the total time required by this plan to complete all tasks.

Input Format

The first line contains two positive integers mm, nn, separated by a space, where mm (<20< 20) is the number of machines and nn (<20< 20) is the number of jobs.

The second line contains m×nm \times n integers separated by spaces, which is the given arrangement order.

The next 2n2n lines each contain mm positive integers separated by spaces, each not exceeding 2020.

The first nn of these lines give, in order, the machine IDs used by each operation of each job. The first number is the machine ID for the 11-st operation, the second number is the machine ID for the 22-nd operation, and so on.

The last nn of these lines give, in order, the processing times of each operation of each job.

You may assume all the above data are correct, and no validation is required.

Output Format

A single positive integer: the minimum total processing time (makespan).

2 3
1 1 2 3 3 2
1 2 
1 2 
2 1
3 2 
2 5 
2 4

10

Hint

NOIP 2006 Senior, Problem 3.

Translated by ChatGPT 5