Advertisement
i_love_rao_khushboo

Untitled

Aug 15th, 2022
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | None | 0 0
  1. #define ll long long
  2. #define ld long double
  3. #define ull unsigned long long
  4. #define pb push_back
  5. #define ppb pop_back
  6. #define pf push_front
  7. #define ppf pop_front
  8. #define mp make_pair
  9. #define F first
  10. #define S second
  11. #define PI 3.1415926535897932384626
  12. #define sz(x) ((int)(x).size())
  13. #define vset(v, n, val) v.clear(); v.resize(n, val)
  14.  
  15. typedef pair<int, int> pii;
  16. typedef pair<ll, ll> pll;
  17. typedef vector<int> vi;
  18. typedef vector<ll> vll;
  19. typedef vector<ull> vull;
  20. typedef vector<bool> vb;
  21. typedef vector<char> vc;
  22. typedef vector<string> vs;
  23. typedef vector<pii> vpii;
  24. typedef vector<pll> vpll;
  25. typedef vector<vi> vvi;
  26. typedef vector<vll> vvll;
  27. typedef vector<vull> vvull;
  28. typedef vector<vb> vvb;
  29. typedef vector<vc> vvc;
  30. typedef vector<vs> vvs;
  31.  
  32. const int INF = 1e9;
  33.  
  34. class Solution {
  35. public:
  36.    
  37.     // adjacency matrix representation of graph
  38.     vvi g;
  39.  
  40.     // d[i][j] => shortest path length b/w vertices i & j
  41.     vvi d;
  42.  
  43.     // nxt[][] is used to retrieve the actual path
  44.     // nxt[i][j] => vertex which is just next to vertex 'i' in the
  45.     //              shortest path from i to j
  46.     // vvi nxt;
  47.  
  48.     // it will be true if there is -ve weight cycle in the graph
  49.     // bool neg_cycle;
  50.  
  51.     // #vertices
  52.     int n;
  53.  
  54.     // function to retrive the shortest paths (if exist) betweeen each pair of vertices
  55.     // ref: https://www.geeksforgeeks.org/finding-shortest-path-between-any-two-nodes-using-floyd-warshall-algorithm/
  56. //     void retrieve_paths() { 
  57. //         for(int i = 0; i < n; i++) {
  58. //             for(int j = 0; j < n; j++) {
  59. //                 cout << "For " << i << " to " << j << ": \n";
  60. //                 // no shortest path exist if i and j part of -ve wt cycle
  61. //                 if(d[i][j] == -INF) {
  62. //                     cout << "No shortest path exists\n\n";
  63. //                     continue;
  64. //                 }
  65.  
  66. //                 // if path exist retrieve it
  67. //                 int x = i, y = j;
  68. //                 cout << x;
  69. //                 if(x == y) {
  70. //                     cout << "\n\n";
  71. //                     continue;
  72. //                 }
  73.  
  74. //                 cout << " -> ";
  75.  
  76. //                 while(x != y) {
  77. //                     x = nxt[x][y];
  78. //                     cout << x;
  79. //                     if(x != y) cout << " -> ";
  80. //                 }
  81.  
  82. //                 cout << "\n\n";
  83. //             }
  84. //         }
  85. //     }
  86.  
  87.     // If required use long long instead of int data type,
  88.     // 0-based indexing of vertices is used
  89.     void floyd_warshall() {
  90.         // initialisation of d[][] and nxt[][] arrays
  91.         for(int i = 0; i < n; i++) {
  92.             for(int j = 0; j < n; j++) {
  93.                 if(i == j) d[i][j] = 0;
  94.                 else {
  95.                     if(g[i][j] == INF) d[i][j] = INF;
  96.                     else d[i][j] = g[i][j];
  97.                 }
  98.             }
  99.         }
  100.  
  101.         // running main logic of the algorithm
  102.         for(int i = 0; i < n; i++) {
  103.             for(int j = 0; j < n; j++) {
  104.                 for(int k = 0; k < n; k++) {
  105.                     if(d[i][k] == INF or d[k][j] == INF) continue;
  106.                     if(d[i][k] + d[k][j] < d[i][j]) {
  107.                         d[i][j] = d[i][k] + d[k][j];
  108.                     }
  109.                 }
  110.             }
  111.         }
  112.  
  113. //         neg_cycle = 0;
  114.  
  115. //         // to check if there is -ve weight cycle in the graph,
  116. //         // therefore if d[i][j] == -INF, then pairs [i][j] don't have a shortest path between them
  117. //         // ref: https://cp-algorithms.com/graph/finding-negative-cycle-in-graph.html
  118. //         for(int i = 0; i < n; i++) {
  119. //             for(int j = 0; j < n; j++) {
  120. //                 for(int k = 0; k < n; k++) {
  121. //                     if(d[i][k] < INF and d[k][k] < 0 and d[k][j] < INF) {
  122. //                         d[i][j] = -INF;
  123. //                         neg_cycle = 1;
  124. //                     }
  125. //                 }
  126. //             }
  127. //         }
  128.  
  129. //         retrieve_paths();
  130.     }
  131.    
  132.     int findTheCity(int cities, vector<vector<int>>& edges, int distanceThreshold) {
  133.         n = cities;
  134.        
  135.         g.clear();
  136.         g.resize(n, vi(n, INF));
  137.        
  138.         d.clear();
  139.         d.resize(n, vi(n));
  140.        
  141.         for(int i = 0; i < sz(edges); i++) {
  142.             int u = edges[i][0], v = edges[i][1], wt = edges[i][2];
  143.             g[u][v] = wt;
  144.             g[v][u] = wt;
  145.         }
  146.        
  147.         floyd_warshall();
  148.        
  149.         int res = -1, mn = INF;
  150.        
  151.         for(int i = 0; i < n; i++) {
  152.             int cnt = 0;
  153.            
  154.             for(int j = 0; j < n; j++) {
  155.                 if(i == j) continue;
  156.                 if(d[i][j] <= distanceThreshold) cnt += 1;
  157.             }
  158.            
  159.             if(cnt <= mn) {
  160.                 res = i;
  161.                 mn = cnt;
  162.             }
  163.         }
  164.            
  165.         return res;
  166.     }
  167. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement