GRAPHS22 - Graph basics concepts II

no tags 

Description:

In this problem, your program will have to read a weighted graph (directed or non directed), and will perform one of three algorithms (topological sort, Prim, Floyd) on the graph (a letter will indicate the operation to be performed).

 

Input

The graph to be read comes in several lines: in the first line, you will have a value d: 0 (if the graph is a non directed graph), or 1 (if it is a directed one).

In the second line you will have a pair of numbers: n and e, the first is the quantity of nodes in the graph, and the second is the quantity of edges in the graph.

Then, you will have e lines, with triplets: o, t and w, where o is the source node of an edge, t is the target node, and w is the weight.

The triplets are ordered from the edges with origin at the root node (number 1), in an ascending way. Also, if there are more than one edge starting from one node, they are ordered by target node.

Finally, in the last line, comes a line with a unique letter: g, h, or i. Letter g means your program will have to perform a topological sort (the output will consist of the numbers of the nodes following a topological sorting (smallest numbered available vertex first). Letter h means your program will have to perform a Prim algorithm (the output will consist of two lines: the total cost of the minimun spanning tree, and the list of the edges for that tree -ordered from lower to higher cost-). Letter i means your program will have to perform a Floyd algorithm (the output will consist of the matrix of distances among all the nodes. Take a look at the examples below.

The numbers of the nodes are consecutive: 1, 2, ...n

Output

You will have to show the right output for the graph and the letter read from the input. Take a look at the Examples below to see the format to follow.

Examples:

 
INPUTOUTPUT
1
8 9
1 4 1
1 6 1
2 5 1
3 4 1
3 5 1
4 7 1
5 6 1
5 7 1
5 8 1
g
1 2 3 4 5 6 7 8

INPUTOUTPUT
0
6 9
1 2 5
1 3 30
2 3 20
2 4 10
3 4 10
3 5 15
4 5 5
4 6 20
5 6 15
h
45
1-2 4-5 2-4 3-4 5-6

INPUTOUTPUT
1
4 8
1 2 2
1 4 6
2 3 3
3 1 8
3 2 1
4 1 5
4 2 7
4 3 4
i
0 2 5 6
11 0 3 17
8 1 0 14
5 5 4 0



Added by:Coach UTN FRSF
Date:2011-07-06
Time limit:1s-2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64