結果

問題 No.1344 Typical Shortest Path Sum
ユーザー 👑 emthrmemthrm
提出日時 2021-01-16 13:08:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 3,255 bytes
コンパイル時間 2,530 ms
コンパイル使用メモリ 211,436 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-18 09:14:45
合計ジャッジ時間 5,072 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 3 ms
4,376 KB
testcase_41 AC 3 ms
4,376 KB
testcase_42 AC 3 ms
4,376 KB
testcase_43 AC 3 ms
4,376 KB
testcase_44 AC 4 ms
4,380 KB
testcase_45 AC 3 ms
4,376 KB
testcase_46 AC 3 ms
4,376 KB
testcase_47 AC 3 ms
4,376 KB
testcase_48 AC 3 ms
4,376 KB
testcase_49 AC 3 ms
4,376 KB
testcase_50 AC 3 ms
4,376 KB
testcase_51 AC 3 ms
4,380 KB
testcase_52 AC 3 ms
4,376 KB
testcase_53 AC 3 ms
4,376 KB
testcase_54 AC 3 ms
4,380 KB
testcase_55 AC 3 ms
4,380 KB
testcase_56 AC 3 ms
4,380 KB
testcase_57 AC 3 ms
4,376 KB
testcase_58 AC 4 ms
4,380 KB
testcase_59 AC 3 ms
4,380 KB
testcase_60 AC 2 ms
4,376 KB
testcase_61 AC 1 ms
4,380 KB
testcase_62 AC 2 ms
4,380 KB
testcase_63 AC 2 ms
4,376 KB
testcase_64 AC 2 ms
4,376 KB
testcase_65 AC 2 ms
4,380 KB
testcase_66 AC 2 ms
4,376 KB
testcase_67 AC 2 ms
4,376 KB
testcase_68 AC 2 ms
4,380 KB
testcase_69 AC 2 ms
4,376 KB
testcase_70 AC 1 ms
4,376 KB
testcase_71 AC 1 ms
4,380 KB
testcase_72 AC 1 ms
4,376 KB
testcase_73 AC 2 ms
4,380 KB
testcase_74 AC 2 ms
4,376 KB
testcase_75 AC 2 ms
4,376 KB
testcase_76 AC 2 ms
4,380 KB
testcase_77 AC 2 ms
4,380 KB
testcase_78 AC 2 ms
4,380 KB
testcase_79 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T>
struct WarshallFloyd {
  std::vector<std::vector<T>> graph, dist;

  WarshallFloyd(const std::vector<std::vector<T>> &graph, const T TINF) : graph(graph), dist(graph), TINF(TINF) {
    n = graph.size();
    internal.assign(n, std::vector<int>(n, -1));
    for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
      if (dist[i][j] > dist[i][k] + dist[k][j]) {
        dist[i][j] = dist[i][k] + dist[k][j];
        internal[i][j] = k;
      }
    }
  }

  void add(int src, int dst, T cost) {
    srcs.emplace_back(src);
    dsts.emplace_back(dst);
    costs.emplace_back(cost);
  }

  void calc() {
    std::set<int> vers;
    int sz = srcs.size();
    for (int i = 0; i < sz; ++i) {
      int s = srcs[i], t = dsts[i];
      T cost = costs[i];
      if (cost < graph[s][t]) graph[s][t] = cost;
      if (dist[s][t] >= cost) {
        dist[s][t] = cost;
        internal[s][t] = -1;
      }
      vers.emplace(s);
      vers.emplace(t);
    }
    for (int v : vers) {
      for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
        if (dist[i][j] > dist[i][v] + dist[v][j]) {
          dist[i][j] = dist[i][v] + dist[v][j];
          internal[i][j] = v;
        }
      }
    }
    srcs.clear();
    dsts.clear();
    costs.clear();
  }

  bool has_negative_cycle() const {
    for (int i = 0; i < n; ++i) {
      if (dist[i][i] < 0) return true;
    }
    return false;
  }

  std::vector<int> build_path(int s, int t) const {
    std::vector<int> res;
    if (dist[s][t] != TINF) {
      build_path(s, t, res);
      res.emplace_back(t);
    }
    return res;
  }

private:
  const T TINF;
  int n;
  std::vector<std::vector<int>> internal;
  std::vector<int> srcs, dsts;
  std::vector<T> costs;

  void build_path(int s, int t, std::vector<int> &path) const {
    int k = internal[s][t];
    if (k == -1) {
      path.emplace_back(s);
    } else {
      build_path(s, k, path);
      build_path(k, t, path);
    }
  }
};

int main() {
  int n, m; cin >> n >> m;
  vector g(n, vector(n, LINF));
  REP(i, n) g[i][i] = 0;
  while (m--) {
    int s, t; ll d; cin >> s >> t >> d; --s; --t;
    chmin(g[s][t], d);
  }
  WarshallFloyd wf(g, LINF);
  REP(i, n) {
    ll ans = 0;
    REP(j, n) {
      if (wf.dist[i][j] < LINF / 2) ans += wf.dist[i][j];
    }
    cout << ans << '\n';
  }
  return 0;
}
0