#P16561. [ICPC 2026 APC] Upside Down Dijkstra
[ICPC 2026 APC] Upside Down Dijkstra
题目描述
Your little sibling has a connected undirected graph of vertices and edges. The vertices are numbered from to , and the edges are numbered from to . Edge connects vertices and with a positive integer weight of .
Your little sibling coded Dijkstra's algorithm to find the shortest distances from vertex to all other vertices, as shown in the pseudocode below. The array records the order in which each vertex is popped from the heap for the first time. Note that even though tuples for each vertex may be pushed multiple times into the heap, each vertex is added to exactly once.
However, your little sibling made a big mistake. In your sibling's code, the heap always pops the maximum tuple instead of the minimum tuple. Here, the heap orders tuples lexicographically by , with larger considered larger; ties are broken by larger .

Your little sibling shares the graph structure with you, that is, all pairs of and ( ). However, your sibling does not share the edge weights . Instead, your sibling shares an array with you and asks you to reconstruct the edge weights. Your task is to find integer edge weights ( for all ) such that running your sibling's incorrect code yields the given array .
Find any such assignment of edge weights. If no such assignment exists, report that it is impossible.
输入格式
The first line of input contains two integers and ( ; ).
The -th of the next lines contains two integers and ( ; for all ). The input guarantees that the graph is connected.
The next line contains integers ( ; for all ).
输出格式
If no assignment of edge weights yields the given array , output impossible.
Otherwise, output a single line containing integers ( ) for the weights of the edges such that your sibling's incorrect code yields the given array .
If there are multiple outputs, any one of them will be accepted.
It can be shown that if there exists any assignment of positive integer weights that yields , then there also exists one with for all .
5 7
3 4
2 3
1 2
3 5
1 4
1 5
4 5
1 4 3 5 2
6 1 3 1 3 2 2
4 4
1 2
2 3
1 3
2 4
1 3 4 2
impossible
提示
Explanation for the sample input/output #1
Figure C.1 illustrates the structure of the graph and the assignment of edge weights in the sample output.
Figure C.1: Graph with assigned edge weights.With this assignment, your sibling's incorrect code runs as follows:
- Initially, the heap contains .
- From , vertex is popped from the heap for the first time. Now the heap contains .
- From , vertex is popped. Now the heap contains .
- From , vertex is popped. Now the heap contains $\{(15, 4), (10, 5), (10, 2), (6, 1), (5, 5), (3, 2), (2, 5)\}$ .
- From , vertex is popped. Now the heap contains $\{(12, 4), (12, 1), (11, 3), (10, 2), (6, 1), (5, 5), (3, 2), (2, 5)\}$ .
- From , vertex is popped.
This results in .