Fastrail08

Longest Path in DAG: TOPOLOGICAL SORT - IMPORTANT

Sep 6th, 2025
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector<int> kahn(int src, int n, vector<vector<pair<int, int> > > &adj){
  5.     queue<int> q;
  6.     vector<int> indegree(n, 0);
  7.     vector<int> order;
  8.    
  9.     for(int i = 0; i < adj.size(); i++){
  10.         for(int j = 0; j < adj[i].size(); j++){
  11.             int v = adj[i][j].first;
  12.             indegree[v]++;
  13.         }
  14.     }
  15.    
  16.     for(int i = 0; i < n; i++){
  17.         if(indegree[i] == 0){
  18.             q.push(i);
  19.         }
  20.     }
  21.    
  22.     while(!q.empty()){
  23.         int front = q.front();
  24.         q.pop();
  25.        
  26.         order.push_back(front);
  27.        
  28.         for(int i = 0; i < adj[front].size(); i++){
  29.             int node = adj[front][i].first;
  30.             indegree[node]--;
  31.             if(indegree[node] == 0){
  32.                 q.push(node);
  33.             }
  34.         }
  35.     }
  36.    
  37.     vector<int> distance(n, INT_MIN);
  38.     //process in topological order - guarantees that proccesing of node u, will be done after all it's prerequisites have already been processed
  39.     //OR PROCESSED/EXPLORED all the paths that can reach u. So if all the paths are considered, whatever property was to be stored, is stored(min distance, max distance, reachability, paths(better to store predecessors tho, and track backwards to destination nodes))
  40.     distance[src] = 0;
  41.     for(int k = 0; k < order.size(); k++){
  42.         int node = order[k];
  43.         //distance from source != INT_MAX, that means it MAY or MAY NOT be able to relax an edge. But it is a possibility.
  44.         //distance[X] = INT_MAX, these X nodes are not reachable from source, so they obviously won't be able to RELAX other nodes distances from source(Relaxation happens when nodes give a path to the source to reach other nodes at a better distance), since these X nodes are unreachable, they won't be able to give such paths(shorter/longer, here longer), simply no relaxation can't happen
  45.        
  46.         if(distance[node] != INT_MIN){
  47.             //Relaxation possible - Try Relaxing all edges from this node
  48.             for(int i = 0; i < adj[node].size(); i++){
  49.                 int child = adj[node][i].first, dist = adj[node][i].second;
  50.                 //found a better distance / larger distance
  51.                 distance[child] = max(distance[child], distance[node] + dist);
  52.             }
  53.         }
  54.     }
  55.     return distance;
  56. }
  57.  
  58. vector<int> maximumDistance(vector<vector<int> > &edges, int n, int m, int src){
  59.     vector<vector<pair<int, int> > > adj(n, vector<pair<int, int> >());
  60.     for(int i = 0; i < edges.size(); i++){
  61.         int u = edges[i][0], v = edges[i][1], w = edges[i][2];
  62.         adj[u].emplace_back(v, w);
  63.     }
  64.     return kahn(src, n, adj);
  65. }
  66.  
  67. int main() {
  68.     // your code goes here
  69.     int n, m, src;
  70.     cin >> n >> m >> src;
  71.     vector<vector<int> > edges(m, vector<int>(3));
  72.     for(int i = 0; i < edges.size(); i++){
  73.         cin >> edges[i][0] >> edges[i][1] >> edges[i][2];
  74.     }
  75.    
  76.     vector<int> distance = maximumDistance(edges, n, m, src);
  77.     for(int i = 0; i < distance.size(); i++){
  78.         cout << "Node: " << i << " Distance: " << distance[i] << '\n';
  79.     }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment