結果

問題 No.2569 はじめてのおつかいHard
ユーザー 👑 OnjoujiTokiOnjoujiToki
提出日時 2023-12-03 06:32:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 184 ms / 2,000 ms
コード長 1,971 bytes
コンパイル時間 1,576 ms
コンパイル使用メモリ 140,724 KB
実行使用メモリ 26,380 KB
最終ジャッジ日時 2023-12-03 06:32:46
合計ジャッジ時間 4,095 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
9,856 KB
testcase_01 AC 49 ms
9,856 KB
testcase_02 AC 49 ms
9,856 KB
testcase_03 AC 52 ms
9,856 KB
testcase_04 AC 49 ms
9,856 KB
testcase_05 AC 174 ms
25,828 KB
testcase_06 AC 133 ms
16,392 KB
testcase_07 AC 143 ms
26,100 KB
testcase_08 AC 184 ms
26,380 KB
testcase_09 AC 91 ms
13,416 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>

void solve() {
  int n, m;
  std::cin >> n >> m;
  std::vector<std::vector<std::pair<long long, int>>> g(n), rg(n);

  for (int i = 0; i < m; i++) {
    int u, v;
    long long w;
    std::cin >> u >> v >> w;
    u--, v--;
    g[u].emplace_back(w, v);
    rg[v].emplace_back(w, u);
  }

  using info = std::pair<long long, int>;

  auto cal = [&](std::vector<std::vector<std::pair<long long, int>>>& g,
                 int start) {
    std::priority_queue<info, std::vector<info>, std::greater<info>> pq;
    std::vector<long long> dp(n, 1e16);
    pq.emplace(0, start);
    dp[start] = 0;
    while (!pq.empty()) {
      auto [d, u] = pq.top();
      pq.pop();

      if (dp[u] == d) {
        for (auto [w, v] : g[u]) {
          if (dp[v] > dp[u] + w) {
            dp[v] = dp[u] + w;
            pq.emplace(dp[v], v);
          }
        }
      }
    }
    return dp;
  };

  auto d1 = cal(rg, n - 1);
  auto d2 = cal(rg, n - 2);
  auto d3 = cal(g, n - 1);
  auto d4 = cal(g, n - 2);
  auto p = [&](std::vector<long long>& A) {
    for (auto& x : A) std::cout << x << ' ';
    std::cout << '\n';
  };

  for (int k = 0; k < n - 2; k++) {
    long long ans = 1e16;
    long long a = d1[k] + d3[n - 2] + d4[k];  // k-> n-1 -> n-2 -> k
    long long b = d2[k] + d4[n - 1] + d3[k];  //  k-> n-2 -> n-1 -> k;

    ans = std::min({ans, a, b});
    if (ans >= 1e16) ans = -1;
    std::cout << ans << '\n';
  }
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int t = 1;
  // std::cin >> t;
  while (t--) {
    solve();
  }
}
0