Professional Documents
Culture Documents
Group: A2
Question:
Write a code for finding all the shortest path from single source to all nodes on a DAG using
Topological Sort.
CODE:
#include<bits/stdc++.h>
vector<pair<int,int>>gr[200];
bool visited[200];
stack<int>rst;
vector<int>dis;
void DFS(int src)
visited[src]=1;
int neig=gr[src][i].first;
if(visited[neig]==0)
{
DFS(neig);
rst.push(src);
{
int dist[N];
dist[i] = 1e9;
dist[src] = 0;
while(!rst.empty())
rst.pop();
if (dist[node] != INF) {
for(auto it : gr[node]) {
void print(stack<int>stk)
while(!stk.empty())
int x = stk.top();
stk.pop();
}
cout << endl;
int main()
int n,m;
cin>>n>>m;
int s;
cin>>s;
for(int i=0; i<m; i++)
int x,y,w;
cin>>x>>y>>w;
gr[x].push_back({y,w});
if(visited[i]==0)
DFS(i);
print(rst);
shortestPath(s,n);
}