Conference Rides

면접 대비

시간 제한1초메모리 제한2048 MB

요약
차가 있는 참가자마다 차 없는 참가자를 최대 한 명씩 태워 먼저 내려주도록 배정해, 모든 참가자가 집에 도착하는 최대 시간을 최소화한다.
난이도

보통10점 중 6점

유형
그리디, 정렬, 수학
정답자
아직 제출이 없습니다

문제

There are nn attendees in a conference, numbered 11 through nn. Each of the first mm attendees (numbered 11 through mm) has a car to drive home after the event. The remaining n−mn - m attendees who do not have a car, are going to get a ride to their homes with the help of the first mm attendees. Each of the first mm attendees can pick up at most one other attendee (from attendees m+1m + 1 to nn) and drive them to their house before going to their own home. You are given the distance matrix DD of the n+1n + 1 locations (the conference hall and nn attendees’ homes). Find a way for attendees with cars to drive the attendees without cars home, such that the time it takes for all attendees to arrive at their homes is minimized. The distance matrix DD is an (n+1)×(n+1)(n+ 1) \times (n+ 1) matrix where D\[i]\[j]D\[i]\[j] denotes the estimated time of transportation from location ii to location jj. Location ii (for 1≤i≤n1 \le i \le n) denotes the home of the iith attendee and the conference hall is positioned at location n+1n + 1.

입력

The input starts with a line containing two integers nn and mm, (1≤n≤5001 \le n \le 500 and 1≤m≤n1 \le m \le n). It is guaranteed that 2m≥n2m \ge n.

The following n+1n+1 lines specify the distance matrix DD, each containing n+1n+1 integers. The jjth number from the i+1i + 1th line of the input (for 1≤i,j≤n+11 \le i, j \le n + 1) specifies D\[i]\[j]D\[i]\[j] (0≤D\[i]\[j]≤1080 \le D\[i]\[j] \le 10^8). It is guaranteed that D\[i]\[k]≤D\[i]\[j]+D\[j]\[k]D\[i]\[k] \le D\[i]\[j] + D\[j]\[k] for any 1≤i,j,k≤n+11 \le i, j, k \le n + 1, and also D\[i]\[j]=0D\[i]\[j] = 0 for i=ji = j, but D\[i]\[j]D\[i]\[j] is not necessarily equal to D\[j]\[i]D\[j]\[i].

출력

In the first line of output, print the minimum time it takes for all attendees to arrive at their homes. In the next mm lines, each line ii (for 1≤i≤m1 \le i \le m) should contain a single non-negative integer t_it\_i, denoting the driving schedule of the iith attendee. If t_i=0t\_i = 0, the attendee drives directly to their home without picking up any other attendees. Otherwise (t_i>0t\_i > 0), the iith attendee picks up the attendee t_it\_i and takes them to their home before driving to their own home. Each attendee must be transferred by exactly one car.

예제1

  1. 예제 1

    입력
    3 2
    0 1 1 2
    2 0 1 3
    4 2 0 4
    4 3 2 0
    
    예상 출력
    4
    0
    3