-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdijkstra_modified_stp_with_even_vertices.cpp
More file actions
134 lines (118 loc) · 3.02 KB
/
dijkstra_modified_stp_with_even_vertices.cpp
File metadata and controls
134 lines (118 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
const int MAXX = 10000, INF = 1e9;
// Adjacency List: to represent graph
vector<vector<pair<int, int>>>
adj(MAXX * 10 + 3);
// Distance Array: to store shortest
// distance to every node
vector<int> dist(MAXX * 10 + 3, INF);
// returns value which will
// represent even_x
int even(int x)
{
return x * 10 + 2;
}
// returns value which will
// represent odd_x
int odd(int x)
{
return x * 10 + 1;
}
// converting edge (a->b) to 2
// different edges i.e. (a->b)
// converts to (1). even_a -> odd_b
// (2). odd_a -> even_b
// since, graph is undirected, so we
// push them in reverse order too
// hence, 4 push_back operations are
// there.
void addEdge(int a, int b, int cost)
{
adj[even(a)].push_back({odd(b), cost});
adj[odd(a)].push_back({even(b), cost});
adj[odd(b)].push_back({even(a), cost});
adj[even(b)].push_back({odd(a), cost});
}
// Function calculates shortest
// distance to all nodes from
// "source" using Dijkstra
// Shortest Path Algorithm
// and returns shortest distance
// to "destination"
int dijkstra(int source, int destination)
{
/* Priority Queue/min-heap
to store and process
(distance, node) */
priority_queue<pair<int, int>> pq;
// pushing source node to
// priority queue and dist from
// source to source is set to 0
pq.push({0, even(source)});
dist[even(source)] = 0;
while (!pq.empty())
{
// U is the node at top
// of the priority queue
// note that pq.top().first
// refers to the Distance
// and pq.top().second
// will refer to the Node
int u = pq.top().second;
pq.pop();
// exploring all neighbours
// of node u
for (pair<int, int> p :
adj[u])
{
/* v is neighbour node of u
and c is the cost/weight
of edge (u, v) */
int v = p.first;
int c = p.second;
// relaxation: checking if there
// is a shorter path to v via u
if (dist[u] + c < dist[v])
{
// updating distance of v
dist[v] = dist[u] + c;
pq.push({-dist[v], v});
}
}
}
// returning shortest
// distance to "destination"
return dist[even(destination)];
}
// Driver function
int main()
{
// n = number of Nodes,
// m = number of Edges
int n, m;
while (cin >> n >> m)
{
while (m--)
{
int x, y, w;
cin >> x >> y >> w;
addEdge(x, y, w);
}
int source = 1;
int destination = n;
int ans = dijkstra(source, destination);
// if ans is INF: There is no
// even length path from source
// to destination else path
// exists and we print the
// shortest distance
if (ans == INF)
cout << "-1"
<< "\n";
else
cout << ans << "\n";
}
return 0;
}