This page is still under construction.

Parts of this page are still being built. What you see may change.

Sea Journey

Interview

Time limit1sMemory limit128 MB

Summary
Process a stream of queries where each query asks for the shortest path between two islands after a sequence of edge insertions.
Level

Medium6 of 10

Topics
Shortest path, Graph, Dynamic programming, Simulation
Solved
No attempts yet

Problem

The country of JOI has nn islands, numbered from 11 to nn. A network of boat routes connecting the islands is being developed.

You work at a ticket office that handles boat tickets. Many people in JOI want to travel between islands as cheaply as possible by boat, and they send you order forms listing a departure island and a destination island.

Your job is: as soon as you receive an order form, compute the cheapest possible fare among all ways of traveling from the departure to the destination by transferring between boats, and report it to the customer.

Depending on the route, however, it may be impossible to travel by boat at all. In that case you must answer −1-1, meaning the trip is impossible. In addition, new boat routes between islands keep starting service in JOI, and you are told about them as they appear. Every answer you give must reflect the most up-to-date information.

Given the customers' order forms and the information about newly started boat routes as input, write a program that produces the answer for each order form.

Input

The first line contains two integers nn and kk (1≤n≤1001 \le n \le 100, 1≤k≤50001 \le k \le 5000): there are nn islands, followed by kk command lines.

Each of the next kk lines contains either 33 or 44 integers separated by spaces.

  • If the first number is 00, the line is a customer order form.
    • The line contains three integers 00, aa, bb (1≤a≤n1 \le a \le n, 1≤b≤n1 \le b \le n, a≠ba \ne b).
    • It means a customer sent an order form with departure island aa and destination island bb.
  • If the first number is 11, the line describes a boat route that has just started service.
    • The line contains four integers 11, cc, dd, ee (1≤c≤n1 \le c \le n, 1≤d≤n1 \le d \le n, c≠dc \ne d, 1≤e≤10000001 \le e \le 1000000).
    • A boat running back and forth between islands cc and dd has started service; the fare from cc to dd and the fare from dd to cc are both ee.
    • This boat must be taken into account for all order forms after this line.

Initially no boats are in service. At most 10001000 of the input lines describe boat routes. Note that several different boats may operate between the same pair of islands.

Output

Let mm be the number of order-form lines in the input.

Print mm lines. Line ii (1≤i≤m1 \le i \le m) contains an integer: the answer to the ii-th order form.

That is, if it is possible to travel from the departure to the destination of the ii-th order form by transferring between boats, print the minimum total fare; if it is impossible, print −1-1.

Explanation

The figure below illustrates, for the first input, how the boat routes start service one by one and the answer given for each order form.

Examples4

  1. Example 1

    Input
    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
    
    Expected output
    -1
    15
    12
    
  2. Example 2

    Input
    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
    
    Expected output
    5955
    21431
    9298
    16392
    24774
    8840
    
  3. Example 3

    Input
    2 3
    0 1 2
    1 1 2 100
    0 1 2
    
    Expected output
    -1
    100
    
  4. Example 4

    Input
    4 6
    1 1 2 5
    1 2 3 5
    1 3 4 5
    0 1 4
    0 4 1
    0 1 3
    
    Expected output
    15
    15
    10