結果

問題 No.1393 Median of Walk
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-02-12 22:06:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 2,495 bytes
コンパイル時間 865 ms
コンパイル使用メモリ 104,228 KB
実行使用メモリ 14,108 KB
最終ジャッジ日時 2023-09-27 04:57:17
合計ジャッジ時間 2,961 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 11 ms
4,380 KB
testcase_09 AC 10 ms
4,380 KB
testcase_10 AC 10 ms
4,380 KB
testcase_11 AC 30 ms
13,868 KB
testcase_12 AC 23 ms
11,828 KB
testcase_13 AC 19 ms
9,728 KB
testcase_14 AC 29 ms
13,880 KB
testcase_15 AC 25 ms
13,964 KB
testcase_16 AC 28 ms
13,932 KB
testcase_17 AC 18 ms
9,792 KB
testcase_18 AC 28 ms
13,960 KB
testcase_19 AC 28 ms
14,108 KB
testcase_20 AC 28 ms
14,004 KB
testcase_21 AC 41 ms
13,868 KB
testcase_22 AC 27 ms
13,864 KB
testcase_23 AC 20 ms
9,832 KB
testcase_24 AC 26 ms
13,816 KB
testcase_25 AC 21 ms
9,776 KB
testcase_26 AC 13 ms
4,380 KB
testcase_27 AC 14 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 5 ms
4,380 KB
testcase_30 AC 14 ms
5,460 KB
testcase_31 AC 14 ms
5,532 KB
testcase_32 AC 12 ms
9,700 KB
testcase_33 AC 10 ms
7,664 KB
testcase_34 AC 26 ms
13,860 KB
testcase_35 AC 21 ms
11,764 KB
testcase_36 AC 18 ms
9,764 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 10 ms
7,924 KB
testcase_39 AC 11 ms
7,668 KB
testcase_40 AC 9 ms
4,384 KB
testcase_41 AC 9 ms
4,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }

constexpr int MAX_N = 1010;
constexpr int MAX_M = 2510;

struct Edge {
  int a, b, c;
};

int N, M;
Edge E[MAX_M];

vector<int> G[MAX_N];

int S[MAX_M];
int dist[MAX_N];
int que[10'000'010], *qb, *qe;
bool on[MAX_N];

void shortest(int src, int c0) {
  if (chmin(dist[src], c0)) {
    qb = qe = que;
    *qe++ = src;
    on[src] = true;
    for (; qb != qe; ) {
      const int u = *qb++;
      on[u] = false;
      for (const int i : G[u]) {
        const int v = E[i].b;
        const int cc = max(dist[u] + S[i], -(M + 1));
        if (chmin(dist[v], cc)) {
          if (!on[v]) {
            *qe++ = v;
            on[v] = true;
          }
        }
      }
    }
  }
}

int ans[MAX_N];

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    for (int i = 0; i < M; ++i) {
      scanf("%d%d%d", &E[i].a, &E[i].b, &E[i].c);
      --E[i].a;
      --E[i].b;
    }
    sort(E, E + M, [&](const Edge &e0, const Edge &e1) {
      return (e0.c < e1.c);
    });
    
    fill(G, G + N, vector<int>{});
    for (int i = 0; i < M; ++i) {
      G[E[i].a].push_back(i);
    }
    fill(S, S + M, +1);
    fill(dist, dist + N, M + 1);
    shortest(0, 0);
    
    fill(ans, ans + N, -1);
    for (int i = 0; i < M; ++i) {
      S[i] = -1;
      shortest(E[i].b, dist[E[i].a] - 1);
// cerr<<"dist = ";pv(dist,dist+N);
      for (int u = 1; u < N; ++u) {
        if (dist[u] <= 0) {
          if (ans[u] == -1) {
            ans[u] = E[i].c;
          }
        }
      }
    }
    for (int u = 1; u < N; ++u) {
      printf("%d\n", ans[u]);
    }
cerr<<"========"<<endl;
  }
  return 0;
}
0