Skip to content

Commit 368d12f

Browse files
authored
Add files via upload
1 parent 333bd74 commit 368d12f

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// author: 4yush933
2+
// problem: https://cses.fi/problemset/task/1667/
3+
// Sol: https://cses.fi/problemset/result/1028819/
4+
// Question: Finding shortest path between two nodes in a graph
5+
#pragma GCC optimize("O3")
6+
#include <bits/stdc++.h>
7+
#include <fstream>
8+
using namespace std;
9+
typedef long long ll;
10+
#define F first
11+
#define pii pair<int,int>
12+
#define S second
13+
#define endl "\n"
14+
#define gcd(a,b) __gcd(a,b)
15+
#define lcm(a,b) (a*b)/gcd(a,b)
16+
#define mem(z,i) memset(z,i,sizeof(z))
17+
#define eps 1e-7
18+
#define I (int)
19+
#define mod 1000000007
20+
#define pb push_back
21+
const long long INF = 1000000000000000000LL;
22+
template <typename Type>
23+
ostream &operator<<(ostream &out, vector<Type> &vec) {
24+
for (auto val : vec)
25+
out << val << " ";
26+
return out;
27+
}
28+
//const int WASTE=∞;
29+
ll power(ll a,ll b){
30+
ll i=1;
31+
while(b){
32+
if(b%2)
33+
i=(i*a)%mod;
34+
b/=2;
35+
a=(a*a)%mod;
36+
}
37+
return i;
38+
}
39+
ll mulmod(ll a, ll b) {
40+
ll res = 0;
41+
a = a % mod;
42+
while (b > 0) {
43+
if (b % 2 == 1)
44+
res = (res + a) % mod;
45+
a = (a * 2) % mod;
46+
b /= 2;
47+
}
48+
return res % mod;
49+
}
50+
const int N=1e5+5;
51+
signed main() {
52+
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
53+
// cout<<fixed;
54+
// cout.precision(10);
55+
// ifstream cin ("alchemy_input.txt");
56+
// ofstream cout ("out.txt");
57+
//#define int ll
58+
int n,m;
59+
cin>>n>>m;
60+
vector<int>adj[N];
61+
while(m--){
62+
int a,b;
63+
cin>>a>>b;
64+
adj[a].pb(b); adj[b].pb(a);
65+
}
66+
int d[n+1],p[n+1];
67+
for(int x=2;x<=n;x++)
68+
d[x]=mod;
69+
queue<int>q;
70+
q.push(1); d[1]=1;
71+
while(!q.empty()){
72+
int x=q.front(); q.pop();
73+
for(int y:adj[x]){
74+
if(d[y]==mod) q.push(y);
75+
if(d[x]+1<d[y]) d[y]=d[x]+1,p[y]=x;
76+
}
77+
}
78+
if(d[n]==mod) cout<<"IMPOSSIBLE";
79+
else{
80+
cout<<d[n]<<endl;
81+
int i=n; vector<int>v;
82+
while(i!=1){
83+
v.pb(i); i=p[i];
84+
}
85+
v.pb(1);
86+
reverse(begin(v),end(v));
87+
cout<<v;
88+
}
89+
return 0;
90+
}
91+
// Profile:https://cses.fi/user/13405

0 commit comments

Comments
 (0)