結果

問題 No.1301 Strange Graph Shortest Path
ユーザー m_tsubasa
提出日時 2020-11-27 22:15:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,361 bytes
コンパイル時間 2,461 ms
コンパイル使用メモリ 209,916 KB
最終ジャッジ日時 2025-01-16 07:38:50
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 28 WA * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define inf (long long)(1e17)
using namespace std;

using P = pair<long long, long long>;

int n, m;
vector<map<int, long long>> g, g2;
priority_queue<P, vector<P>, greater<P>> pq;

long long solve();

int main() {
  cin >> n >> m;
  g.resize(n);
  g2.resize(n);
  for (int i = 0; i < m; ++i) {
    int x, y, c, d;
    cin >> x >> y >> c >> d;
    --x, --y;
    g[x][y] = g[y][x] = c;
    g2[x][y] = g2[y][x] = d;
  }
  cout << solve() << endl;
  return 0;
}

long long solve() {
  long long res = 0;
  auto dijk = [](vector<map<int, long long>> &g, vector<long long> &dist,
                 vector<long long> &par) {
    dist.assign(n, inf);
    par.assign(n, -1);
    pq.push(P(0, 0));
    dist[0] = 0;
    while (pq.size()) {
      auto [d, now] = pq.top();
      pq.pop();
      if (d != dist[now]) continue;
      for (auto [to, cost] : g[now])
        if (d + cost < dist[to]) {
          dist[to] = d + cost;
          par[to] = now;
          pq.push(P(dist[to], to));
        }
    }
    return dist[n - 1];
  };
  vector<long long> dist, par;
  res += dijk(g, dist, par);
  int now = n - 1;
  while (now != 0) {
    int to = par[now];
    g[to][now] = g[now][to] = -1;
    now = to;
  }
  for (int i = 0; i < n; ++i)
    for (auto [to, cost] : g[i])
      if (cost >= 0) g2[i][to] = cost;
  return res + dijk(g2, dist, par);
}
0