From 4245cec44059454a3d4c8f8f3d9d29ee6748870a Mon Sep 17 00:00:00 2001 From: piyushsehgal21 <44509954+piyushsehgal21@users.noreply.github.com> Date: Sat, 27 Oct 2018 02:12:19 +0530 Subject: [PATCH] Single Source Shortest Path in a DAG --- SSSP_in_a_DAG.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 SSSP_in_a_DAG.cpp diff --git a/SSSP_in_a_DAG.cpp b/SSSP_in_a_DAG.cpp new file mode 100644 index 0000000..7c61db8 --- /dev/null +++ b/SSSP_in_a_DAG.cpp @@ -0,0 +1,86 @@ + //All trees with directed edges are automatically DAGs as they do not contains any cycle. + //Single Source Shortest Path(SSSP) can be solved efficiently on DAG in O(V+E). + //SSSP on DAG + + #include + using namespace std; + + #define pb push_back + #define MAX 100001 + #define INF (1<<20) + #define pii pair + + bool visited[MAX]; + vector G[MAX]; + vector topological; + int D[MAX]; + + void dfs(int node){ + + visited[node] = true; + + for(int i=0 ; i D[u] + w){ + D[v] = D[u] + w; + } + } + + int main() { + + int edges,nodes; + cin>>nodes>>edges; + + memset(visited,false,sizeof(visited)); + //memset(D,INF,sizeof(D)); + for(int i=0 ; i>u>>v>>w; + G[u].pb(pii(v,w)); + } + + + for(int i=0 ; i