Problem A. Dijkstra
Statement
You are to write a program that receives a weighted directed graph and finds
distances from source vertex S to all other vertices. Distance from S to
some vertex W is the minimal length of path going from S to W.
Length of path is the sum of weights of its edges.
Vertices are numbered with integers from 1 to N.
Input file format
First line of input file contains three integers
N M and
S,
where
M is the number of edges. Next
M lines contain three
integers each —
starting vertex number, ending vertex number and weight of some edge respectively.
All weights are positive.
There is at most one edge connecting two vertices in every direction.
Output file format
Output file must contain
N integers — distances from
source vertex to all vertices.
If some vertices are not reachable from
S, corresponding numbers must be −1.
Constraints
1 ≤ N ≤ 1000.
All weights are less or equal than 1000.
Sample tests
No. |
Input file (input.txt ) |
Output file (output.txt ) |
1 |
5 3 1
1 2 5
1 3 7
3 4 10
|
0 5 7 17 -1
|
Problem B. Floyd-Warshall
Statement
You are to write a program that finds shortest distances between all pairs
of vertices in a directed weighted graph.
Graph consists of
N vertices, numbered from 1 to
N, and
M edges.
Input file format
Input file contains two integers
N and
M, followed my
M triplets of integers
ui vi wi — starting vertex, ending vertex and weight or the edge.
There is at most one edge connecting two vertices in every direction. There are no cycles of negative weight.
Output file format
Output file must contain a matrix of size
Nx
N.
Element in the
j-th column of
i-th row mush be the shortest distance between
vertices
i and
j.
The distance from the vertex to itself is considered to be 0.
If some vertex is not reachable from some other,
there must be empty space in corresponding cell of matrix.
Constraints
0 ≤
N ≤ 100.
All weights are less than 1000 by absolute value.
Sample tests
No. |
Input file (input.txt ) |
Output file (output.txt ) |
1 |
3 3
1 2 5
1 3 10
2 3 2
|
0 5 7
0 2
0
|
Problem C. Ford-Bellman
Statement
You are to write a program that finds shortest distances from vertex S
to all other vertices in a given directed weighted graph.
Graph consists of
N vertices, numbered from 1 to
N, and
M edges.
Input file format
Input file contains two integers
N M S, followed my
M triplets of integers
ui vi wi — starting vertex, ending vertex and weight or the edge.
There is at most one edge connecting any two vertices in every direction. There are no cycles of negative weight.
Output file format
Output file must contain a vector of
N integers — distances from vertex
S to other vertices.
The distance from any vertex to itself is considered to be 0.
If some vertex is not reachable from
S, corresponding cell of the vector must contain empty space.
Constraints
0 ≤
N,
M ≤ 1000.
All weights are less than 1000 by absolute value.
Sample tests
No. |
Input file (input.txt ) |
Output file (output.txt ) |
1 |
3 3 1
1 2 5
1 3 10
2 3 2
|
0 5 7
|
Problem D. Shortest Spanning Tree
Statement
You are to write a program that receives a weighted undirected graph and finds length of its shortest spanning tree.
Input file format
Input file contains two integers N, M.
Vertices are numbered with integer numbers from 1 to N. M is the number of edges. Each of next M lines contain three
integers describing an edge — numbers of vertices, connected by an edge and its weight respectively. All weights are
positive. There is at most one edge connecting two vertices.
Output file format
Output file must contain a signle integer number — length of the SST. If it is impossible to construct spanning tree,
output file must contain −1.
Constraints
1 ≤ N ≤ 1000
All weights are less or equal 1000.
Sample tests
No. |
Input file (input.txt ) |
Output file (output.txt ) |
1 |
3 3
1 2 10
2 3 10
3 1 10
|
20
|
Problem E. SST for sparse graph
Statement
You are to write a program that receives a weighted undirected graph and finds length of its shortest spanning tree.
Input file format
Input file contains two integers N, M.
Vertices are numbered with integer numbers from 1 to N. M is the number of edges. Each of next M lines contain three
integers describing an edge — numbers of vertices, connected by an edge and its weight respectively. All weights are
positive. There is at most one edge connecting two vertices.
Output file format
Output file must contain a signle integer number — length of the SST. If it is impossible to construct spanning tree,
output file must contain −1.
Constraints
1 ≤ N, M ≤ 100000
All weights are less or equal 1000.
Sample tests
No. |
Input file (input.txt ) |
Output file (output.txt ) |
1 |
3 3
1 2 10
2 3 10
3 1 10
|
20
|