This page is still under construction.

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

Minimum weight spread for strong connectivity

Time limit2sMemory limit512 MB

Summary
Pick a strongly connected subgraph of a complete directed graph so the chosen edges' weight range is as small as possible.
Level

Medium6 of 10

Topics
Graph, Sorting, Two pointers, Union-find
Solved
No attempts yet

Problem

There is a directed weighted graph with NN vertices numbered 0 to N−1N-1. For every ordered pair of distinct vertices xx and yy there is one edge from xx to yy, so the graph has N×(N−1)N \times (N-1) edges in total.

Choose some of these edges to build a new graph. Using only the chosen edges, a path from any vertex uu to any vertex vv must always exist. For example, when N=3N=3 you may choose 0⇒10 \Rightarrow 1, 1⇒01 \Rightarrow 0, 0⇒20 \Rightarrow 2, 2⇒02 \Rightarrow 0, and you may also choose 0⇒10 \Rightarrow 1, 1⇒21 \Rightarrow 2, 2⇒02 \Rightarrow 0.

Among the chosen edges, make the difference between the largest weight and the smallest weight as small as possible. Find the minimum of that difference over all valid choices. When N=1N=1 there is no edge to choose, so the answer is 0.

Input

The first line contains NN, a natural number at most 50.

Each of the next NN lines holds one row of the weight matrix. The y+1y+1-th value from the left on the x+1x+1-th of these lines is the weight of the edge from vertex xx to vertex yy. Every weight is an integer between 0 and 150,000. The self loop from vertex xx to vertex xx is always given as 0, and self loops cannot be chosen.

Output

Print one integer, the minimum possible difference between the largest and the smallest chosen weight.

Examples2

  1. Example 1

    Input
    4
    0 11 13 13
    10 0 12 13
    10 10 0 11
    12 10 10 0
    
    Expected output
    1
    
  2. Example 2

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