Submission

Status:

[PPPPPPPPPPPPPPPPPPPP]

Subtask/Task Score:

{100/100}

Score: 100

User: colo39

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

Language: cpp

Time: 0.011 second

Submitted On: 2026-11-20 22:46:42

#include <bits/stdc++.h>
using namespace std;

vector<pair<int,int>> adj1[405];
bool ch[405][405];
vector<pair<int,int>> adj2[405];
int dist1[405];
int dist2[405];
int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,m;
    cin >> n >> m;
    for (int i = 0;i < m;i++) {
        int u,v;
        cin >> u >> v;
        adj1[u].push_back({v,10*abs(v-u)});
        adj1[v].push_back({u,10*abs(v-u)});
        ch[u][v] = 1;
        ch[v][u] = 1;
    }
    for (int i = 1;i <= n;i++) {
        for (int j = i+1;j <= n;j++) {
            if (!ch[i][j]) {
                adj2[i].push_back({j,10*abs(i-j)});
                adj2[j].push_back({i,10*abs(i-j)});
                ch[i][j] = 1;
                ch[j][i] = 1;
            }
        }
    }
    priority_queue<pair<int,int>,vector<pair<int,int>>, greater<>> pq;
    pq.push({0,1});
    for (int i = 0; i<= n+1;i++) {
        dist1[i] = 1e9;
        dist2[i] = 1e9;
    }
    dist1[1] = 0;
    while (!pq.empty()) {
        auto u = pq.top();
        pq.pop();
        if (u.first > dist1[u.second]) continue;
        for (auto &x : adj1[u.second]) {
            pair<int,int> p = {x.second + dist1[u.second],x.first};
            if (p.first >= dist1[p.second]) continue;
            dist1[p.second] = p.first;
            pq.push(p);
        }
    }

    pq.push({0,1});
    dist2[1] = 0;
    while (!pq.empty()) {
        auto u = pq.top();
        pq.pop();
        if (u.first > dist2[u.second]) continue;
        for (auto &x : adj2[u.second]) {
            pair<int,int> p = {x.second + dist2[u.second],x.first};
            if (p.first >= dist2[p.second]) continue;
            dist2[p.second] = p.first;
            pq.push(p);
        }
    }
    if (max(dist1[n],dist2[n]) == 1e9) cout << -1;
    else cout << max(dist1[n],dist2[n]);



    return 0;
}