#P7685. [CEOI 2005] Mobile Service
[CEOI 2005] Mobile Service
Description
A company provides services to its partners located in different towns. The company currently has mobile service workers. If a service request occurs at some location, a worker must move from their current location to the request location (if no worker is already there) to fulfill the request. At any time, only one worker may move. They can move only when requested, and multiple workers are not allowed to be in the same location. Moving a worker from location to location incurs a certain cost . The cost is not necessarily symmetric, but staying still costs , i.e., . The company must handle the received requests strictly in first-come-first-served order.
Please write a program that decides which worker should move for each request, so that the total cost of serving the given list of requests is as small as possible.
Input Format
The first line contains two integers and . is the number of locations, and is the number of requests. Locations are represented by integers from to . Each of the next lines contains non-negative integers. The -th number in line is the cost . The last line contains integers, the list of requests. Each request is identified by the location where it occurs. Initially, the three service workers are at locations and , and these are also used as the identifiers of the three workers.
Output Format
The first line contains an integer , the minimum total cost to serve the list of requests. The second line contains exactly integers. The -th number is the identifier of the worker ( or ) who will serve the -th request.
5 9
0 1 1 1 1
1 0 2 3 2
1 1 0 4 1
2 1 5 0 1
4 2 3 4 0
4 2 4 1 5 4 3 2 1
5
1 2 1 2 2 1 3 1 3
Hint
Constraints
For of the testdata, , , .
Notes
From Mobile Service, CENTRAL-EUROPEAN OLYMPIAD IN INFORMATICS 2005.
Translated and organized by
Translated by ChatGPT 5
京公网安备 11011102002149号