結果

問題 No.1301 Strange Graph Shortest Path
ユーザー SSRSSSRS
提出日時 2020-11-27 22:54:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,979 bytes
コンパイル時間 1,926 ms
コンパイル使用メモリ 182,896 KB
実行使用メモリ 19,260 KB
最終ジャッジ日時 2023-10-09 21:47:13
合計ジャッジ時間 14,839 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
11,532 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 213 ms
15,928 KB
testcase_03 AC 181 ms
14,532 KB
testcase_04 AC 243 ms
18,924 KB
testcase_05 AC 191 ms
16,340 KB
testcase_06 AC 222 ms
17,196 KB
testcase_07 AC 220 ms
16,420 KB
testcase_08 AC 178 ms
14,384 KB
testcase_09 AC 213 ms
16,784 KB
testcase_10 AC 189 ms
14,468 KB
testcase_11 AC 227 ms
17,192 KB
testcase_12 AC 230 ms
17,656 KB
testcase_13 AC 228 ms
16,672 KB
testcase_14 AC 211 ms
16,188 KB
testcase_15 AC 208 ms
16,100 KB
testcase_16 AC 248 ms
19,072 KB
testcase_17 AC 230 ms
17,052 KB
testcase_18 AC 207 ms
15,612 KB
testcase_19 AC 227 ms
17,464 KB
testcase_20 AC 225 ms
17,880 KB
testcase_21 AC 222 ms
17,000 KB
testcase_22 AC 247 ms
18,516 KB
testcase_23 AC 227 ms
16,748 KB
testcase_24 AC 225 ms
17,984 KB
testcase_25 AC 239 ms
18,176 KB
testcase_26 AC 218 ms
16,924 KB
testcase_27 AC 226 ms
16,640 KB
testcase_28 AC 196 ms
15,064 KB
testcase_29 AC 254 ms
19,260 KB
testcase_30 AC 251 ms
17,824 KB
testcase_31 AC 241 ms
18,156 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 144 ms
14,356 KB
testcase_34 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000;
int main(){
  int N, M;
  cin >> N >> M;
  vector<vector<tuple<int, int, int>>> E(N);
  for (int i = 0; i < M; i++){
    int u, v, c, d;
    cin >> u >> v >> c >> d;
    u--;
    v--;
    E[u].push_back(make_tuple(v, c, d));
    E[v].push_back(make_tuple(u, c, d));
  }
  vector<long long> d1(N, INF);
  priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq1;
  pq1.push(make_pair(0, 0));
  while (!pq1.empty()){
    long long dd = pq1.top().first;
    int v = pq1.top().second;
    pq1.pop();
    if (d1[v] > dd){
      d1[v] = dd;
      for (auto edge : E[v]){
        int w = get<0>(edge);
        if (d1[w] > dd + get<1>(edge)){
          pq1.push(make_pair(dd + get<1>(edge), w));
        }
      }
    }
  }
  vector<int> pr(N, -1);
  int t = N - 1;
  while (t > 0){
    for (auto edge : E[t]){
      int w = get<0>(edge);
      if (d1[w] == d1[t] - get<1>(edge)){
        pr[t] = w;
      }
    }
    t = pr[t];
  }
  vector<vector<pair<long long, int>>> E2(N);
  for (int i = 0; i < N; i++){
    for (auto edge : E[i]){
      int w = get<0>(edge);
      if (pr[i] != w && pr[w] != i){
        E2[i].push_back(make_pair(w, get<1>(edge)));
      } else if (i == pr[w]){
        E2[i].push_back(make_pair(w, get<2>(edge)));
      } else {
        E2[i].push_back(make_pair(w, -get<1>(edge)));
      }
    }
  }
  vector<long long> d2(N, INF);
  priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq2;
  pq2.push(make_pair(0, 0));
  while (!pq2.empty()){
    long long dd = pq2.top().first;
    int v = pq2.top().second;
    pq2.pop();
    if (d2[v] > dd){
      d2[v] = dd;
      for (auto edge : E2[v]){
        int w = edge.first;
        if (d2[w] > dd + edge.second){
          pq2.push(make_pair(dd + edge.second, w));
        }
      }
    }
  }
  cout << d1[N - 1] + d2[N - 1] << endl;
}
0