Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views3 pages

Assignment 10 Daa

The document describes an assignment by Anikeit Sethi that implements Dijkstra's Algorithm to find the shortest distance from a starting city to all other cities in a graph. The provided C++ code initializes a graph using adjacency lists, processes the input, and calculates the shortest paths using a priority queue. The time complexity of the algorithm is O(N + M log(N)), where N is the number of vertices and M is the number of edges.

Uploaded by

devanandsv312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Assignment 10 Daa

The document describes an assignment by Anikeit Sethi that implements Dijkstra's Algorithm to find the shortest distance from a starting city to all other cities in a graph. The provided C++ code initializes a graph using adjacency lists, processes the input, and calculates the shortest paths using a priority queue. The time complexity of the algorithm is O(N + M log(N)), where N is the number of vertices and M is the number of edges.

Uploaded by

devanandsv312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ASSIGNMENT - 10

ANIKEIT SETHI (190001003)

 Code Logic: - We are given a starting city and we have to find the shortest
distance to every city from this city. So, this problem is similar to the single
source shortest path problem. We can just use Dijkstra’s Algorithm to achieve
the result. Below I have implemented the Dijkstra’s algorithm using priority
queue.

Code:-
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define rf(i, a, b) for (ll i = a; i >= b; i--)
typedef std::vector<long long> vi;
#define F first
#define S second
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define mod 1000000007
#define PB push_back
#define MP make_pair
#define PI 3.14159265358979323846
#define all(a) a.begin(), a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))
const ll INF = LLONG_MAX / 2;
const ll N = 2e5 + 1;
using namespace std;

int main()
{
int n, m;
cout << "Input:->\n";
cin >> n >> m;
vector<pair<int, int>> adj[n + 1];
for (int i = 1; i <= m; i++)
{
ll a, b, c;
cin >> a >> b >> c;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
int distance[n + 1];
for (int i = 1; i <= n; i++)
distance[i] = INT_MAX;
distance[1] = 0;
priority_queue<pair<int, int>> pq;
int mark[n + 1] = {0};
pq.push({0, 1});
while (!pq.empty())
{
pair<int, int> s = pq.top();
pq.pop();
int source = s.second, dist = s.first;
mark[source] = 1;
for (auto it : adj[source])
{
int x = it.first, edge_weight = it.second;
if (mark[x] == 1)
continue;
if (distance[source] + edge_weight < distance[x])
{
distance[x] = distance[source] + edge_weight;
pq.push({-distance[x], x});
}
}
}
cout << "OUTPUT->\n";
for (int i = 2; i <= n; i++)
cout << "1 " << i << " " << distance[i] << "\n";
}

Output: -

Time Complexity: - O (N + M log(N)) where n is the number of vertices


and m is the number of edges.
Graph: -

You might also like