From 7040e27fb255985c713f70960c226b7521234700 Mon Sep 17 00:00:00 2001 From: Nidheesh Pandey Date: Fri, 13 Oct 2017 16:07:47 +0000 Subject: [PATCH 1/3] Create Dijkstra.cpp --- Graph/Dijkstra.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Graph/Dijkstra.cpp diff --git a/Graph/Dijkstra.cpp b/Graph/Dijkstra.cpp new file mode 100644 index 000000000..448d4b500 --- /dev/null +++ b/Graph/Dijkstra.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; +vector < pair > graph[5*100001]; +int m,n,l,x,y; +int dis[5*100001]; +int dij(vector > * v,int s,int * dis) { + priority_queue < pair , vector < pair >,greater < pair > > pq; + pq.push(make_pair(0,s)); + dis[s] = 0; + int u; + while(!pq.empty()) { + + u = (pq.top()).second; + pq.pop(); + for( vector > :: iterator it = v[u].begin(); it != v[u].end();it++) { + if(dis[u] + it->first < dis[it->second]) { + dis[it->second] = dis[u] + it->first; + pq.push(make_pair(dis[it->second],it->second)); + } + } + } +} +int main() { + cin>>n>>m; + for(int i = 0;i < m;i++) { + scanf("%d%d%d",&x,&y,&l); + graph[x].push_back(make_pair(l,y)); + graph[y].push_back(make_pair(l,x)); + } + dij(graph,1,dis); + +} From feeca868433c711cba61b6043795222478e99e5b Mon Sep 17 00:00:00 2001 From: Nidheesh Pandey Date: Fri, 13 Oct 2017 21:25:53 +0000 Subject: [PATCH 2/3] Update Dijkstra.cpp --- Graph/Dijkstra.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Graph/Dijkstra.cpp b/Graph/Dijkstra.cpp index 448d4b500..844ad168f 100644 --- a/Graph/Dijkstra.cpp +++ b/Graph/Dijkstra.cpp @@ -1,5 +1,6 @@ #include using namespace std; +#define INF 10000010 vector < pair > graph[5*100001]; int m,n,l,x,y; int dis[5*100001]; @@ -27,6 +28,16 @@ int main() { graph[x].push_back(make_pair(l,y)); graph[y].push_back(make_pair(l,x)); } - dij(graph,1,dis); - + + scanf("%d",&s); + for(int i = 0;i < n;i++) + dis[i] = INF; + dij(graph,s,dis); + + for(int i = 0;i < n;i++) + if(dis[i] == INF) + cout<<"-1 "; + else + cout< Date: Thu, 28 Dec 2017 19:56:35 +0530 Subject: [PATCH 3/3] add comments and remove --- Graph/Dijkstra.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Graph/Dijkstra.cpp b/Graph/Dijkstra.cpp index 844ad168f..c395cd527 100644 --- a/Graph/Dijkstra.cpp +++ b/Graph/Dijkstra.cpp @@ -1,16 +1,18 @@ -#include +#include +#include +#include +#include using namespace std; #define INF 10000010 vector < pair > graph[5*100001]; -int m,n,l,x,y; int dis[5*100001]; int dij(vector > * v,int s,int * dis) { priority_queue < pair , vector < pair >,greater < pair > > pq; + // source distance to zero. pq.push(make_pair(0,s)); dis[s] = 0; int u; while(!pq.empty()) { - u = (pq.top()).second; pq.pop(); for( vector > :: iterator it = v[u].begin(); it != v[u].end();it++) { @@ -22,19 +24,23 @@ int dij(vector > * v,int s,int * dis) { } } int main() { + int m,n,l,x,y,s; + // n--> number of nodes , m --> number of edges cin>>n>>m; for(int i = 0;i < m;i++) { + // input edges. scanf("%d%d%d",&x,&y,&l); graph[x].push_back(make_pair(l,y)); - graph[y].push_back(make_pair(l,x)); + graph[y].push_back(make_pair(l,x)); // comment this line for directed graph } - + // start node. scanf("%d",&s); - for(int i = 0;i < n;i++) + // intialise all distances to infinity. + for(int i = 1;i <= n;i++) dis[i] = INF; dij(graph,s,dis); - for(int i = 0;i < n;i++) + for(int i = 1;i <= n;i++) if(dis[i] == INF) cout<<"-1 "; else