luogu#P8906. [USACO22DEC] Breakdown P

    ID: 3703 远端评测题 3000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>USACO2022最短路均摊分析折半搜索 meet in the middle

[USACO22DEC] Breakdown P

Problem Description

Farmer John’s farm can be represented as a weighted directed graph. Roads (edges) connect different nodes, and the weight of each edge is the time needed to travel along that road. Every day, Bessie likes to go from the barn (located at node 11) to the pasture (located at node NN) using exactly KK roads, and she wants to arrive as quickly as possible under this restriction. However, at certain times, roads stop being maintained and begin to break down one by one, becoming impassable. Help Bessie find the shortest path from the barn to the pasture at every moment.

Formally, we start with a weighted directed complete graph with NN nodes (1N3001 \le N \le 300) and N2N^2 edges: for every pair (i,j)(i,j) with 1i,jN1 \le i,j \le N, there is an edge (note that there are NN self-loops). After each removal of one edge, output the minimum total weight among all paths from 11 to NN that use exactly KK edges (edges on the path do not have to be distinct) (2K82 \le K \le 8). Note that after the ii-th removal, the graph has N2iN^2-i edges remaining.

The weight of a path is defined as the sum of the weights of all edges on the path. Note that a path may contain the same edge multiple times or visit the same node multiple times, including nodes 11 and NN.

Input Format

The first line contains NN and KK.

The next NN lines each contain NN integers. The jj-th integer in the ii-th line is wij(1wij108)w_{ij}(1 \le w_{ij} \le 10^8).

The following N2N^2 lines each contain two integers ii and jj (1i,jN1 \le i,j \le N). Each pair of integers appears exactly once.

Output Format

Output N2N^2 lines. For each edge removal, output the minimum total weight of a path that uses exactly KK edges. If no such path exists, output 1-1.

3 4
10 4 4
9 5 3
2 1 6
3 1
2 3
2 1
3 2
2 2
1 3
3 3
1 1
1 2
11
18
22
22
22
-1
-1
-1
-1

Hint

Sample 1 Explanation

After the first removal, the shortest path that uses exactly 44 edges is:

$$1 \rightarrow 2 \rightarrow 3 \rightarrow 2 \rightarrow 3$$

After the second removal, the shortest path that uses exactly 44 edges is:

$$1 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 3$$

After the third removal, the shortest path that uses exactly 44 edges is:

$$1 \rightarrow 3 \rightarrow 3 \rightarrow 3 \rightarrow 3$$

After the sixth removal, there is no longer any path that uses exactly 44 edges.

Testdata Properties

  • For 2T142 \le T \le 14, testdata TT satisfies K=T+32K=\lfloor \dfrac{T+3}{2} \rfloor

Translated by ChatGPT 5