Skip to content

Commit 55780a2

Browse files
authored
Create 1680 longestFlightRoute(dijkastra).cpp
This code is giving tle but this is also a method to solve this problem basically assign every node -1 weight and apply dijkastra.
1 parent e8f5ef4 commit 55780a2

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// JAI BAJARANG BALI
2+
3+
// manitianajay45
4+
5+
// give me some sunshine, give me some rain, give me another chance to grow up once again....
6+
7+
// sab moh maya hai....
8+
9+
/*
10+
This code is giving tle but this is also a method to solve this problem
11+
basically assign every node -1 weight and apply dijkastra.
12+
*/
13+
14+
#include <bits/stdc++.h>
15+
using namespace std;
16+
17+
#define endl "\n"
18+
19+
#define ll int
20+
21+
22+
int main()
23+
{
24+
ios_base::sync_with_stdio(false);
25+
cin.tie(NULL);
26+
ll n, m;
27+
cin >> n >> m;
28+
vector<ll> graph[n + 1];
29+
vector<ll> par(n + 1, -1);
30+
vector<ll> d(n + 1, 0);
31+
for (ll i = 0; i < m; i++)
32+
{
33+
ll u, v;
34+
cin >> u >> v;
35+
graph[u].push_back(v);
36+
}
37+
d[1] = 0;
38+
par[1]=-1;
39+
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
40+
pq.push({0, 1});
41+
while (!pq.empty())
42+
{
43+
pair<ll, ll> pr = pq.top();
44+
pq.pop();
45+
if (d[pr.second] < pr.first)
46+
{
47+
continue;
48+
}
49+
for (auto i : graph[pr.second])
50+
{
51+
if (d[i] > d[pr.second] - 1)
52+
{
53+
d[i] = d[pr.second] - 1;
54+
pq.push({d[i], i});
55+
par[i] = pr.second;
56+
}
57+
}
58+
}
59+
60+
if (d[n] == 0)
61+
{
62+
cout << "IMPOSSIBLE" << endl;
63+
return 0;
64+
}
65+
cout << (-1 * d[n] + 1) << endl;
66+
vector<ll> ans;
67+
ll nd = n;
68+
while (nd != -1)
69+
{
70+
ans.push_back(nd);
71+
nd = par[nd];
72+
}
73+
reverse(ans.begin(), ans.end());
74+
for (auto i : ans)
75+
{
76+
cout << i << " ";
77+
}
78+
cout << endl;
79+
80+
return 0;
81+
}

0 commit comments

Comments
 (0)