結果

問題 No.1393 Median of Walk
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-02-12 22:09:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 2,526 bytes
コンパイル時間 1,100 ms
コンパイル使用メモリ 107,980 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 04:57:24
合計ジャッジ時間 3,567 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 13 ms
4,380 KB
testcase_09 AC 12 ms
4,376 KB
testcase_10 AC 11 ms
4,380 KB
testcase_11 AC 42 ms
4,380 KB
testcase_12 AC 34 ms
4,380 KB
testcase_13 AC 31 ms
4,380 KB
testcase_14 AC 40 ms
4,376 KB
testcase_15 AC 39 ms
4,376 KB
testcase_16 AC 38 ms
4,380 KB
testcase_17 AC 25 ms
4,384 KB
testcase_18 AC 38 ms
4,376 KB
testcase_19 AC 40 ms
4,380 KB
testcase_20 AC 42 ms
4,376 KB
testcase_21 AC 59 ms
4,380 KB
testcase_22 AC 42 ms
4,376 KB
testcase_23 AC 28 ms
4,376 KB
testcase_24 AC 37 ms
4,380 KB
testcase_25 AC 33 ms
4,376 KB
testcase_26 AC 16 ms
4,376 KB
testcase_27 AC 16 ms
4,380 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 16 ms
4,376 KB
testcase_31 AC 16 ms
4,380 KB
testcase_32 AC 14 ms
4,376 KB
testcase_33 AC 13 ms
4,376 KB
testcase_34 AC 36 ms
4,380 KB
testcase_35 AC 30 ms
4,380 KB
testcase_36 AC 26 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 11 ms
4,376 KB
testcase_39 AC 14 ms
4,376 KB
testcase_40 AC 10 ms
4,376 KB
testcase_41 AC 11 ms
4,380 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 = 10100;
constexpr int MAX_M = 25100;

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];
bool on[MAX_N];

void shortest(int src, int c0) {
  if (chmin(dist[src], c0)) {
    queue<int> que;
    que.push(src);
    on[src] = true;
    for (; !que.empty(); ) {
      const int u = que.front();
      que.pop();
      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]) {
            que.push(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;
#ifndef LOCAL
break;
#endif
  }
  return 0;
}
0