Minimum weight spread for strong connectivity
Time limit2sMemory limit512 MB
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 vertices numbered 0 to . For every ordered pair of distinct vertices and there is one edge from to , so the graph has edges in total.
Choose some of these edges to build a new graph. Using only the chosen edges, a path from any vertex to any vertex must always exist. For example, when you may choose , , , , and you may also choose , , .
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 there is no edge to choose, so the answer is 0.
Input
The first line contains , a natural number at most 50.
Each of the next lines holds one row of the weight matrix. The -th value from the left on the -th of these lines is the weight of the edge from vertex to vertex . Every weight is an integer between 0 and 150,000. The self loop from vertex to vertex 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.