Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated a README #26

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ author of this change is Tathya Kapadia
Changes are done: Swaroopa

#### Hacktoberfest 2022
#### Thoughtworks Hacktober fest 2022
61 changes: 61 additions & 0 deletions fastest_dijkstras.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include<bits/stdc++.h>
using namespace std;
#define ll long long

const int INF = 2000000000;
typedef pair<int, int> PII;

int main(int argc, char const *argv[])
{
int N, s, t;
scanf("%d%d%d", &N, &s, &t);
vector<vector<PII>> edges(N);

for(int i = 0; i < N; i++)
{
int M;
scanf("%d", &M);

for(int j = 0; j < M; j++)
{
int vertex, dist;
scanf("%d%d", &vertex, &dist);
edges[i].push_back(make_pair(dist, vertex));
}
}

// Using the priority Queue in which top element has the smallest priority

priority_queue<PII, vector<PII>, greater<PII>> Q;

vector<int> dist(N, INF), dad(N, -1);
Q.push(make_pair(0, s));

dist[s] = 0;

while(!Q.empty())
{
PII p = Q.top();
Q.pop();

int here = p.second;
if(here == t) break;
if(dist[here] != p.first) continue;

for(vector<PII>::iterator it = edges[here].begin(); it != edges[here].end(); it++)
{
if(dist[here] + it -> first < dist[it -> second])
{
dist[it->second] = dist[here] + it->first;
dad[it->second] = here;
Q.push(make_pair(dist[it->second], it->second));
}
}
}

printf("%d\n", dist[t]);
if (dist[t] < INF)
for (int i = t; i != -1; i = dad[i])
printf("%d%c", i, (i == s ? '\n' : ' '));
return 0;
}