This page is still under construction.

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

Safe Travel

Time limit3sMemory limit128 MB

Summary
For each pasture i, find the shortest path from pasture 1 to i that avoids the last edge of the unique shortest path to i.
Level

Hard8 of 10

Topics
Graph, Shortest path, Segment tree
Solved
No attempts yet

Problem

Gremlins have infested the farm. These nasty, fairy-like creatures love to harass the cows. Every cow starts at the barn, conveniently located at pasture 11, and walks to its own field: cow ii travels from pasture 11 to pasture ii.

Each gremlin knows the unique shortest route its cow normally takes. Gremlin ii waits in the middle of the last road of the shortest route from pasture 11 to pasture ii, hoping to harass cow ii.

To avoid being harassed, each cow ii instead picks the fastest route from pasture 11 (the barn) to pasture ii that does not use that last road of its shortest route. For every cow ii, compute the shortest possible time of such a route that avoids the road guarded by gremlin ii.

  • Pastures are numbered 11 through NN, where 3≤N≤100,0003 \le N \le 100{,}000.
  • Roads are numbered 11 through MM, where 2≤M≤200,0002 \le M \le 200{,}000. Every road is bidirectional.
  • Road ii connects pastures aia_i and bib_i and takes tit_i time to cross (1≤ai,bi≤N1 \le a_i, b_i \le N, 1≤ti≤1,0001 \le t_i \le 1{,}000, ai≠bia_i \ne b_i).
  • No two roads connect the same pair of pastures, and no road connects a pasture to itself.
  • In every test case, the shortest route from pasture 11 to pasture ii is unique.

For example, consider the following pastures and roads (numbers in brackets are traversal times):

      1--[2]--2-------+
      |       |       |
     [2]     [1]     [3]
      |       |       |
      +-------3--[4]--4

Without gremlins, the shortest routes are:

TripBest routeBest timeLast road
1 → 21→221→2
1 → 31→321→3
1 → 41→2→452→4

When each gremlin guards the last road of its cow's shortest route, the best routes that avoid those roads are:

TripNew routeNew best timeRoad to avoid
1 → 21→3→231→2
1 → 31→2→331→3
1 → 41→3→462→4

Input

  • Line 1: Two space-separated integers NN and MM.
  • Lines 2 to M+1M+1: Three space-separated integers aia_i, bib_i, and tit_i.

Output

  • Print N−1N-1 lines. Line ii contains the shortest time to travel from pasture 11 to pasture i+1i+1 without using the last road of the shortest route from pasture 11 to pasture i+1i+1. If no such route exists, print −1-1 alone on that line.

Examples1

  1. Example 1

    Input
    4 5
    1 2 2
    1 3 2
    3 4 4
    3 2 1
    2 4 3
    
    Expected output
    3
    3
    6