Submission

Status:

[PPPPPPPPPPPPPPPPPPPP]

Subtask/Task Score:

{100/100}

Score: 100

User: SnowAveNode

Problemset: รถยนต์ รถไฟ เรือเมล์ ลิเก ตำรวจ

Language: cpp

Time: 0.024 second

Submitted On: 2026-04-12 18:22:50

#include <bits/stdc++.h>
#define ll long long
#define pli pair<ll, int>
using namespace std;

const int nx = 1e5 + 5, MOD = 1e9 + 7, inf = 2e9; const ll INF = 4e18;

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);

    int n, m; cin >> n >> m;
    vector<vector<int>> adj(n+1);
    vector<ll> dist(n+1, INF);
    bool toEnd = false;

    while(m--) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        if((u == 1 && v == n) || (u == n && v == 1)) toEnd = true;
    }

    if(toEnd) {
        vector<vector<int>> tmp(n+1);
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                if(find(adj[i].begin(), adj[i].end(), j) == adj[i].end()) {
                    tmp[i].push_back(j);
                    tmp[j].push_back(i);
                }
        adj = tmp;
    }

    
    priority_queue<pli, vector<pli>, greater<>> pq;
    pq.push({0, 1}); dist[1] = 0;
    while(!pq.empty()) {
        auto [current_distance, u] = pq.top(); pq.pop();
        for(auto v : adj[u]) {
            if(current_distance + 10*abs(u-v) < dist[v]) {
                dist[v] = current_distance + 10*abs(u-v);
                pq.push({dist[v], v});
            }
        }
    }

    cout << (dist[n] == INF ? -1 : dist[n]) << '\n';

    return 0;
}