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

#BS0000434. Bài tập đồ thị khác (Another Exercise on Graphs (Easy 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 (Another Exercise on Graphs (Easy Version))

Another Exercise on Graphs (Easy Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

The instructors of T-generation are preparing a training contest and would like to include a weighted graph problem with the following task.

You are given a connected undirected graph with nn vertices numbered from 11 to nn and mm edges. Each edge joins two distinct vertices and has a positive integer weight. No two edges join the same unordered pair of vertices.

Consider a path from vertex aa to vertex bb consisting of hh edges. Arrange their weights in non-increasing order as w1≥w2≥⋯≥whw_1\ge w_2\ge\cdots\ge w_h. For 1≤k≤h1\le k\le h, the kk-th largest weight on the path is wkw_k. Equal weights are counted with their multiplicities.

There are qq queries. For each query (a,b,k)(a,b,k), determine the minimum possible kk-th largest edge weight among all paths from aa to bb. Every path between the queried vertices is guaranteed to contain at least kk edges.

Input

  • The first line contains three integers n,m,qn,m,q, the numbers of vertices, edges and queries respectively.
  • Each of the next mm lines contains three integers u,v,wu,v,w, describing an undirected edge between uu and vv with weight ww.
  • Each of the next qq lines contains three integers a,b,ka,b,k, describing a query as defined above.

Output

Print qq space-separated integers on one line, in query order. Each integer is the minimum possible kk-th largest edge weight for the corresponding query.

Subtasks

  • Subtask 1 (20 points): 2≤n≤122\le n\le 12; n−1≤m≤min⁡(20,n(n−1)2)n-1\le m\le\min(20,\frac{n(n-1)}{2}); 1≤q≤501\le q\le 50; 1≤u,v≤n1\le u,v\le n, u≠vu\ne v, 1≤w≤1091\le w\le10^9; 1≤a,b≤n1\le a,b\le n, k≥1k\ge1. The graph is connected and undirected, with no self-loops or parallel edges; every path from aa to bb in each query has at least kk edges.

  • 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≤2001\le q\le 200; 1≤u,v≤n1\le u,v\le n, u≠vu\ne v, 1≤w≤1091\le w\le10^9; 1≤a,b≤n1\le a,b\le n, k≥1k\ge1. The graph is connected and undirected, with no self-loops or parallel edges; every path from aa to bb in each query has at least kk edges.

  • Subtask 3 (50 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≤3⋅1051\le q\le 3\cdot10^5; 1≤u,v≤n1\le u,v\le n, u≠vu\ne v, 1≤w≤1091\le w\le10^9; 1≤a,b≤n1\le a,b\le n, k≥1k\ge1. The graph is connected and undirected, with no self-loops or parallel edges; every path from aa to bb in each query has at least kk edges.

Examples

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

The graph has four vertices and four edges. For the first query, the path 1→3→41\to3\to4 has weights 4,14,1, so its second largest weight is 11. No smaller value is possible because all edge weights are positive.

For the second query, the path 2→4→32\to4\to3 has weights 2,12,1, with maximum 22. Every edge incident to vertex 22 weighs 22, so the maximum cannot be smaller. The answers in order are 1 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

The graph has six vertices, seven edges and three queries.

  • For (1,6,3)(1,6,3), the path 1→2→4→5→61\to2\to4\to5\to6 has sorted weights 10,10,2,110,10,2,1, so its third largest weight is 22. To reach vertex 66, a path must take the edge from 11 to 22, cross from {2,3}\{2,3\} to {4,5,6}\{4,5,6\}, and use at least one further edge heavier than 11 to reach 55 or 66. Hence at least three weights exceed 11, and the answer cannot be 11.
  • For (1,6,2)(1,6,2), the path 1→2→3→4→5→61\to2\to3\to4\to5\to6 has sorted weights 10,9,3,2,110,9,3,2,1, giving 99. The first edge weighs 1010, and any route from 22 to 66 must cross an edge from {2,3}\{2,3\} to {4,5,6}\{4,5,6\} weighing at least 99. Thus the second largest weight cannot be below 99.
  • For (2,4,1)(2,4,1), the path 2→3→42\to3\to4 has weights 3,93,9, giving maximum 99. Every route from 22 to 44 must cross an edge weighing at least 99, making this optimal.