Under construction

Still being built. Things may change

Neighbouring Towns

Time limit1sMemory limit128 MB

Summary
Given a symmetric shortest-distance table for n towns, list every pair A,B such that no third town C satisfies dist(A,B) = dist(A,C) + dist(C,B).
Level

Medium4 of 10

Topics
Array, Brute force
Solved
No attempts yet

Problem

A road map comes with a table that gives, for every pair of towns, the length of the shortest route between them. All roads are two-way, so the table is symmetric, and the distance from a town to itself is 00.

The towns have a special property: whenever the shortest distance from town AA to town BB equals the shortest distance from AA to CC plus the shortest distance from CC to BB, some shortest route from AA to BB passes through town CC.

Two towns AA and BB are called neighbouring towns when no other town lies between them on a shortest route, that is, when there is no town CC (different from AA and BB) for which

dist(A,B)=dist(A,C)+dist(C,B).\text{dist}(A, B) = \text{dist}(A, C) + \text{dist}(C, B).

Given the distance table, find every pair of neighbouring towns.

Your program reads the table of distances from standard input, finds all pairs of neighbouring towns, and writes them to standard output.

Input

The first line contains an integer nn (1≤n≤2001 \le n \le 200), the number of towns; the towns are numbered from 11 to nn.

Each of the next nn lines contains nn non-negative integers no greater than 200200, separated by single spaces. The jj-th integer on the ii-th of these lines is the shortest distance between town ii and town jj. The diagonal entries are 00 and the table is symmetric.

Output

Print every pair of neighbouring towns, one pair per line. Within a pair, print the two town numbers in increasing order, separated by a single space. List each pair only once.

Order the lines so that a pair (a,b)(a, b) comes before a pair (c,d)(c, d) when a<ca < c, or when a=ca = c and b<db < d.

If there are no neighbouring pairs, print nothing.

Examples1

  1. Example 1

    Input
    3
    0 1 2
    1 0 3
    2 3 0
    
    Expected output
    1 2
    1 3