Consider a country having 4 states numbered from 1 to 4. These 4 states are connected by 5 bidirectional roads given as :
1 --- 2 with cost = 8
2 --- 3 with cost = 6
3 --- 4 with cost = 5
1 --- 4 with cost = 2
1 --- 3 with cost = 4
The map of the country can be represented as:
Now, the best way to choose 3 roads is:
The cost of travelling from any state to all other states is 2 + 4 + 6 i.e. 12.
The first line contains an integer 'T' denoting the number of test cases or queries to be run.
The first line of each test case or query contains two space-separated integers 'N' and ‘M’ representing the number of states and number of roads in the country, respectively.
The next 'N' lines of every test case contain three single space-separated integers ‘A’, ‘B’ and ‘C’, representing a road between the states 'A' and 'B' and 'C' denoting the cost of travelling them.
For each test case, print 'N' - 1 lines each containing 3 space separated integers 'A', 'B' and 'C' representing the road you have chosen and the cost to traverse that road.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= 'T' <= 10
1 <= 'N' <= 1000
'N' - 1 <= 'M' <= 2000
1 <= 'C' <= 10^6
Time limit: 1 sec
The idea is to create a graph of the country with states as its vertices and roads as edges.
For creating a graph we will use an adjacency matrix representation of graph Cost with Cost[i][j] representing the cost of travelling from state i to state j and vice versa, state j to i.
The very first approach to solve this problem is using Prim’s algorithm, which is a greedy algorithm.
The idea behind Prim’s algorithm is simple, a spanning tree means all states must be connected.
Algorithm:
Let’s understand with the example :
Initially, all the states are marked unvisited and weight values of states are {0, INF, INF, INF}.
Now, pick the state with minimum weight. i.e. state 1 and mark it as visited.
After picking this state, update the weight values of all adjacent states of 1, which are 2 and 4 in this case to their corresponding costs.
The weight array becomes: {0, 3, INF, 5}.
Now, pick the next state with minimum weight which is not visited yet. i.e. state 2 and mark it as visited.
After marking state 2 visited, update the weight value of all the adjacent states of 2 which are not visited yet, i.e state 3 to their corresponding cost if and only if it is less than the one stored in the weight array.
The weight array becomes; {0, 3, 1, 5}.
Similarly, pick the next unvisited state with minimum weight i.e. 3 and mark it as visited.
Update weight values of adjacent states of state 3 which are not visited to their corresponding cost if and only if it is less than the one stored in the weight array.
In the above point, the adjacent states of 3 are 4 and 2.
So, the final MST we get is,
The idea is to again create a graph of the country with states as its vertices and roads as edges.
But this time we will use an adjacency list representation of graph adjList with adjList[i] representing a vector of pair with first element of pair as the neighbour state of i say A, ans second element of pair is the cost of road between i and A.
Ninja and the experiment
COUNT ISLANDS
Distance to a Cycle in Undirected Graph
Search In A Sorted 2D Matrix
Spiral Matrix