PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000444. Bài tập đồ thị khác – bản khó (Another Exercise on Graphs (Hard Version))

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Bài tập đồ thị khác – bản khó (Another Exercise on Graphs (Hard Version))

Another Exercise on Graphs (Hard Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A connected weighted undirected graph has nn vertices numbered from 11 to nn and mm edges, without loops or parallel edges.

For each of the qq queries (a,b,k)(a,b,k), consider paths from aa to bb. If a path has hh edges, arrange their weights in nonincreasing order w1≥w2≥⋯≥whw_1\ge w_2\ge\cdots\ge w_h. The kk-th largest weight is wkw_k, counting equal weights separately.

Find the minimum possible wkw_k over all paths from aa to bb. Every path between the queried vertices has at least kk edges. Queries are independent on the original graph.

Input

  • The first line contains integers n,m,qn,m,q: vertex, edge and query counts.
  • Each of the next mm lines contains integers ui,vi,wiu_i,v_i,w_i, the endpoints and weight of edge ii.
  • Each of the last qq lines contains integers a,b,ka,b,k describing a query.

Output

Print qq space-separated integers on one line in query order.

Subtasks

  • Subtask 1 (20 points): 2≤n≤122\le n\le 12; n−1≤m≤n(n−1)2n-1\le m\le \frac{n(n-1)}{2}; 1≤q≤501\le q\le 50; 1≤ui,vi≤n1\le u_i,v_i\le n, ui≠viu_i\ne v_i, 1≤wi≤1091\le w_i\le10^9 for all 1≤i≤m1\le i\le m; the graph is connected, without parallel edges. For each query, 1≤a,b≤n1\le a,b\le n, 1≤k≤d(a,b)1\le k\le d(a,b), where d(a,b)d(a,b) is the minimum edge count of a path from aa to bb.
  • Subtask 2 (30 points): 2≤n≤4002\le n\le 400; n−1≤m≤min⁡(400,n(n−1)2)n-1\le m\le \min(400,\frac{n(n-1)}{2}); 1≤q≤3000001\le q\le 300000; 1≤ui,vi≤n1\le u_i,v_i\le n, ui≠viu_i\ne v_i, 1≤wi≤1091\le w_i\le10^9 for all 1≤i≤m1\le i\le m; the graph is connected, without parallel edges. For each query, 1≤a,b≤n1\le a,b\le n, 1≤k≤d(a,b)1\le k\le d(a,b), where d(a,b)d(a,b) is the minimum edge count of a path from aa to bb.
  • Subtask 3 (50 points): 2≤n≤4002\le n\le 400; n−1≤m≤n(n−1)2n-1\le m\le \frac{n(n-1)}{2}; 1≤q≤3000001\le q\le 300000; 1≤ui,vi≤n1\le u_i,v_i\le n, ui≠viu_i\ne v_i, 1≤wi≤1091\le w_i\le10^9 for all 1≤i≤m1\le i\le m; the graph is connected, without parallel edges. For each query, 1≤a,b≤n1\le a,b\le n, 1≤k≤d(a,b)1\le k\le d(a,b), where d(a,b)d(a,b) is the minimum edge count of a path from aa to bb.

Examples

Example 1

Input

4 4 2
1 2 2
2 4 2
1 3 4
3 4 1
1 4 2
2 3 1

Output

1 2

Explanation

For (1,4,2)(1,4,2), path 1→3→41\to3\to4 has weights 4 and 1. Its second largest is 1, the minimum edge weight. For (2,3,1)(2,3,1), path 2→4→32\to4\to3 has maximum 2. Both edges incident to 2 have weight 2, so this cannot be improved.

Example 2

Input

6 7 3
1 2 10
2 3 3
3 4 9
4 5 2
5 6 1
2 4 10
4 6 10
1 6 3
1 6 2
2 4 1

Output

2 9 9

Explanation

Query 1: path 1→2→4→5→61\to2\to4\to5\to6 has sorted weights (10,10,2,1)(10,10,2,1), so the third largest is 2. Every path uses edge 1–2, an edge to reach 4 and an edge leaving 4; all three weigh more than 1. Query 2: path 1→2→3→4→5→61\to2\to3\to4\to5\to6 has sorted weights (10,9,3,2,1)(10,9,3,2,1), so the second largest is 9. Edge 1–2 and a crossing edge from {1,2,3}\{1,2,3\} to the rest both weigh at least 9. Query 3: path 2→3→42\to3\to4 has maximum 9; reaching 4 using only edges lighter than 9 is impossible.