Sign in
or
Register
Courses
Textbook
Compiler
Contests
Topics
Courses
Theory of Graphs
Floyd's algorithm
Module:
Floyd's algorithm
Problem
1
/10
Floyd: The Beginning (C++)
Theory
Click to read/hide
Error
Problem
Given a directed graph whose edges are assigned some non-negative weights (lengths). Find the length of the shortest path from vertex s to vertex t.
Input
The first line contains three numbers: the number of vertices in the graph N ≤50, the numbers of vertices s and t. Next comes the adjacency matrix of the graph, that is, N rows, each of which contains N numbers. The j-th number in the i-th row of the adjacency matrix specifies the length of the edge leading from the i-th vertex to the j-th one. Lengths can take any value from 0 to 1000000, the number -1 means that there is no corresponding edge. It is guaranteed that there are zeros on the main diagonal of the matrix.
Output
Print a single number – minimum path length. If the path does not exist, print -1.
Examples
#
Input
Output
1
3 1 2
0 -1 3
7 0 1
2 215 0
218
1000
ms
32 Mb
Rules for program design and list of errors in automatic problem checking
Teacher commentary