結果

問題 No.1995 CHIKA Road
ユーザー yansi819
提出日時 2024-04-14 20:09:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 705 ms / 2,000 ms
コード長 1,231 bytes
コンパイル時間 4,624 ms
コンパイル使用メモリ 269,624 KB
最終ジャッジ日時 2025-02-21 01:58:25
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ld = long double;
using mint = modint998244353;

int main() {
  ll n, m; cin >> n >> m;
  set<ll> st {1, n};
  vector<vector<ll>> edge(m);
  for (ll i = 0; i < m; i++) {
    ll a, b; cin >> a >> b;
    st.insert(a); st.insert(b);
    edge[i] = {a, b};
  }
  ll N = st.size();
  ll cnt = 0, last = -1;
  vector<vector<pair<ll, ll>>> g(N);
  map<ll, ll> mp;
  for (auto it = st.begin(); it != st.end(); it++) {
    mp[*it] = cnt;
    cnt++;
    if (last > -1) {
      g[mp[last]].push_back({mp[*it], 2 * (*it) - 2 * last});
    }
    last = *it;
  }
  for (auto &e: edge) {
    ll a = mp[e[0]], b = mp[e[1]];
    g[a].push_back({b, 2 * e[1] - 2 * e[0] - 1});
  }

  vector<ll> dis(N, 1e18);
  dis[0] = 0;
  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> que;
  que.push({0, 0});
  while (!que.empty()) {
    auto q = que.top(); que.pop();
    ll u = q.second;
    for (auto p: g[u]) {
      ll v = p.first;
      ll c = p.second;
      if (dis[v] > dis[u] + c) {
        dis[v] = dis[u] + c;
        que.push({dis[v], v});
      }
    }
  }
  cout << dis[N - 1] << endl;
  return 0;
}
0