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

#FW0000088. Đường bị phong tỏa (Road Blocked)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đường bị phong tỏa (Road Blocked)

Road Blocked

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

An undirected weighted graph has individually numbered roads. Process operations 1 i (close currently open road ii) and 2 x y (report shortest distance using only open roads, or -1). Each road is closed at most once.

Input

First n,m,qn,m,q; then mm undirected roads uvw‘andu v w` and qoperations‘1i‘or‘2xy‘(1−based; operations `1 i` or `2 x y` (1-based; x<y$ for queries).

Output

For each type-2 query print the current shortest distance or -1, one per line.

Subtasks

  • Subtask 1 (40 points): 2≤n≤7, 1≤q≤102\le n\le7,\ 1\le q\le10. n−1≤m≤n(n−1)/2n-1\le m\le n(n-1)/2, 1≤w≤1091\le w\le10^9; mỗi cạnh chỉ bị xóa tối đa một lần.

  • Subtask 2 (60 points): 2≤n≤30, 1≤q≤202\le n\le30,\ 1\le q\le20. n−1≤m≤n(n−1)/2n-1\le m\le n(n-1)/2, 1≤w≤1091\le w\le10^9; mỗi cạnh chỉ bị xóa tối đa một lần.

Examples

Input

3 3 5
1 2 5
1 3 10
2 3 6
2 1 3
1 2
2 1 3
1 1
2 1 3

Output

10
11
-1

Explanation

Initially d(1,3)=10. Closing road 2 forces the 1–2–3 route of length 11. Closing road 1 then makes the destination unreachable.