Submission

Status:

[PPPPPPPPPP-SSSSSSSSS]

Subtask/Task Score:

{0/100}

Score: 0

User: Hxluk.ka

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

Language: cpp

Time: 0.003 second

Submitted On: 2026-03-08 21:58:20

#include <iostream>
#include <vector>
#include <queue>
#define vt vector
#define pli pair<ll, int>
#define ll long long
using namespace std;
const ll nx=403, inf=4e18;
int n, m, u, v, key[nx][nx];
ll tdist[nx], cdist[nx];
priority_queue<pli, vt<pli>, greater<pli>> pq;
int main() {
    cin.tie(0)->sync_with_stdio(0);
    
    cin>>n>>m;
    for (int i=0; i<n; i++) cin>>u>>v, key[u][v]=key[v][u]=1;
    for (int i=1; i<=n; i++) tdist[i]=inf;
    for (int i=1; i<=n; i++) cdist[i]=inf;
    
    pq.push({0, 1});
    tdist[1]=0;
    while (!pq.empty()) {
        auto [d, c]=pq.top(); pq.pop();
        if (d>tdist[c]) continue;
        for (int i=1; i<=n; i++) {
            if (!key[c][i]||c==i) continue;
            if (tdist[i]>(10ll * abs(i-c))+d) {
                tdist[i]=tdist[c]+(10ll * abs(i-c));
                pq.push({tdist[i], i});
            }
        }
    }

    pq.push({0, 1});
    cdist[1]=0;
    while (!pq.empty()) {
        auto [d, c]=pq.top(); pq.pop();
        if (d>cdist[c]) continue;
        for (int i=1; i<=n; i++) {
            if (key[c][i]||c==i) continue;
            if (cdist[i]>(10ll * abs(i-c))+d) {
                cdist[i]=cdist[c]+(10ll * abs(i-c));
                pq.push({cdist[i], i});
            }
        }
    }

    if (cdist[n]==inf || tdist[n]==inf) cout<<-1;
    else cout<<max(cdist[n], tdist[n]);
}