|
| 1 | +/** |
| 2 | + * Given a graph consisting N nodes (labelled 1 to N) where a specific given node S represents the starting position S |
| 3 | + * and an edge between two nodes is of a given length, which may or may not be equal to other lengths in the graph. |
| 4 | + * It is required to calculate the shortest distance from the start position (Node S) to all of the other nodes in the graph. |
| 5 | + * Note 1: If a node is unreachable , the distance is assumed as −1. |
| 6 | + * |
| 7 | + * Input Format |
| 8 | + * The first line contains T, denoting the number of test cases. |
| 9 | + * First line of each test case has two integers N, denoting the number of nodes in the graph and M, |
| 10 | + * denoting the number of edges in the graph. |
| 11 | + * The next M lines each consist of three space separated integers x y w, where x and y |
| 12 | + * denote the two nodes between which the undirected edge exists, w denotes the length of edge between these corrresponding nodes. |
| 13 | + * The last line has an integer S, denoting the starting position. |
| 14 | + * |
| 15 | + * Output Format |
| 16 | + * For each of the T test cases, Print a single line consisting N−1 space separated integers denoting |
| 17 | + * the shortest distance of N−1 nodes from starting position S. For unreachable nodes, print −1. |
| 18 | + * |
| 19 | + * If there are edges between the same pair of nodes with different weights, |
| 20 | + * they are to be considered as is, like multiple edges. |
| 21 | + */ |
| 22 | + |
| 23 | +#include <cmath> |
| 24 | +#include <cstdio> |
| 25 | +#include <vector> |
| 26 | +#include <iostream> |
| 27 | +#include <algorithm> |
| 28 | +#include <list> |
| 29 | +#include <climits> |
| 30 | +#include <set> |
| 31 | +using namespace std; |
| 32 | + |
| 33 | +vector<int> dist; |
| 34 | + struct lstDist { |
| 35 | + bool operator()(int u, int v) const { |
| 36 | + return make_pair(dist[u], u) < make_pair(dist[v], v); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | +class Graph { |
| 41 | + int N; |
| 42 | + vector<vector<int>> weights; |
| 43 | + int S; |
| 44 | + |
| 45 | + public: |
| 46 | + Graph( int n ) |
| 47 | + : N{n}, weights(N, vector<int>(N, -1)){} |
| 48 | + |
| 49 | + void addEdge( int x, int y, int w) { |
| 50 | + if (weights[x-1][y-1] == -1 || (weights[x-1][y-1] != -1 && weights[x-1][y-1] > w )) { |
| 51 | + weights[x-1][y-1] = w; |
| 52 | + weights[y-1][x-1] = w; |
| 53 | + } |
| 54 | + } |
| 55 | + void setStart(int s) { |
| 56 | + S = s-1; |
| 57 | + dist[S] = 0; |
| 58 | + } |
| 59 | + |
| 60 | + void distance() { |
| 61 | + set<int, lstDist> q; |
| 62 | + q.insert(S); |
| 63 | + while(!q.empty()) { |
| 64 | + int u = *q.begin(); |
| 65 | + q.erase(q.begin()); |
| 66 | + for( int v = 0; v < N ; ++v ) { |
| 67 | + if (weights[u][v] != -1) { |
| 68 | + int newDist = dist[u] + weights[u][v]; |
| 69 | + if (newDist < dist[v]) { |
| 70 | + dist[v] = newDist; |
| 71 | + if (q.count(v)) { |
| 72 | + q.erase(v); |
| 73 | + } |
| 74 | + q.insert(v); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + void printDistance() { |
| 82 | + for ( unsigned int i = 0; i < dist.size(); ++i ) { |
| 83 | + if ( int(i) != S) { |
| 84 | + if (dist[i] == INT_MAX) dist[i] = -1; |
| 85 | + std::cout << dist[i] << " "; |
| 86 | + } |
| 87 | + } |
| 88 | + std::cout << std::endl; |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +int main() { |
| 93 | + int T, N, M, x, y, w, S; |
| 94 | + cin >> T; |
| 95 | + while( T ) { |
| 96 | + cin >> N >> M; |
| 97 | + dist.resize(N); |
| 98 | + for( int i = 0; i < N; ++i) { |
| 99 | + dist[i] = INT_MAX; |
| 100 | + } |
| 101 | + Graph G(N); |
| 102 | + for ( int i = 0; i < M; ++i) { |
| 103 | + cin >> x >> y >> w; |
| 104 | + G.addEdge(x, y, w); |
| 105 | + } |
| 106 | + cin >> S; |
| 107 | + G.setStart(S); |
| 108 | + G.distance(); |
| 109 | + G.printDistance(); |
| 110 | + --T; |
| 111 | + } |
| 112 | + return 0; |
| 113 | +} |
| 114 | + |
0 commit comments