Submission
Status:
[PPPPPPPPPPPPPPPPPPPP]
Subtask/Task Score:
{100/100}
Score: 100
User: august
Problemset: รถยนต์ รถไฟ เรือเมล์ ลิเก ตำรวจ
Language: cpp
Time: 0.009 second
Submitted On: 2026-03-11 13:11:20
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int,int>
const int INF = 1e18, N = 401;
bool g[N][N];
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
int n,m;
cin>> n>> m;
for (int i=0; i<m; i++) {
int u,v;
cin>> u>> v;
g[u][v] = g[v][u] = 1;
}
vector<int> dist(n+1, INF), dist1(n+1, INF);
priority_queue<pi, vector<pi>, greater<pi>> pq;
pq.push({0, 1});
dist[1] = 0;
while (!pq.empty()) {
pi cur = pq.top();
pq.pop();
int d=cur.first, u=cur.second;
if (d > dist[u]) continue;
for (int v=1; v<=n; v++) {
if (!g[u][v] || v==u) continue;
if (dist[v] > dist[u]+10*abs(v-u)) {
dist[v] = dist[u]+10*abs(v-u);
pq.push({dist[v], v});
}
}
}
pq.push({0, 1});
dist1[1] = 0;
while (!pq.empty()) {
pi cur = pq.top();
pq.pop();
int d=cur.first, u=cur.second;
if (d > dist1[u]) continue;
for (int v=1; v<=n; v++) {
if (g[u][v] || v==u) continue;
if (dist1[v] > dist1[u]+10*abs(v-u)) {
dist1[v] = dist1[u]+10*abs(v-u);
pq.push({dist1[v], v});
}
}
}
if (dist[n] == INF || dist1[n] == INF) cout<< -1;
else cout<< max(dist[n], dist1[n]);
}