luogu#P2683. [AHOI2015 初中组] 小岛
[AHOI2015 初中组] 小岛
Background
In the frigid lands of northern Siberia lies an archipelago consisting of small islands, which we number from to in order.
Problem Description
At first, there are no routes between the islands. Later, as transportation develops, routes connecting pairs of islands gradually appear. For example, when a route between island and island is added with travel time , people on island can travel to island , and people on island can also travel to island , with time along this route.
Meanwhile, with the growth of tourism, more and more people come to visit. The shortest path between two islands has therefore become a topic of great interest.
Input Format
The input has lines.
The first line contains two integers and , representing the number of islands and the total number of operations, respectively.
Each of the next lines describes one operation, in one of the following formats:
0 s t: Query the shortest travel time from island to island (, , ).
1 u v e: Add a new bidirectional route between island and island with travel time (, , , ).
Output Format
For each query, output one line.
For each query, if no feasible path exists, output -1; otherwise, output the shortest travel time.
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
-1
15
12
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
5955
21431
9298
16392
24774
8840
Hint
For of the testdata, and .
For of the testdata, and .
For of the testdata, and .
For of the testdata, and .
For of the testdata, and .
Translated by ChatGPT 5