PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FW0000065. Luôn đến muộn (Always Late)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Luôn đến muộn (Always Late)
Always Late
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
For each pair s,t in an undirected positively weighted graph, find the minimum length of a walk whose length is strictly greater than the shortest possible length. Revisiting vertices or edges is allowed.
Input
First n m; then m undirected roads u v w (0-based); then q and q lines s t.
Output
For each query print the strictly second-smallest walk length, or ? if no such walk exists.
Subtasks
- Subtask 1 (40 points): $2\le n\le 8,\ 0\le m\le 18,\ 1\le q\le 12,\ 1\le w\le 100$. Indices are 0-based, u != v. Roads are undirected, possibly parallel; repeated vertices or roads are allowed along a walk.
- Subtask 2 (60 points): $2\le n\le 100,\ 0\le m\le 1000,\ 1\le q\le 100,\ 1\le w\le 100$. Indices are 0-based, u != v. Roads are undirected, possibly parallel; repeated vertices or roads are allowed along a walk.
Examples
Input
3 3
0 1 2
1 2 2
0 2 6
3
0 2
0 0
2 2
Output
6
4
4
Explanation
Shortest 0→2 costs 4 via vertex 1; the strictly next length is 6 via the direct edge. The empty walk 0→0 costs 0, while 0→1→0 costs 4. Likewise 2→1→2 costs 4.