結果

問題 No.468 役に立つ競技プログラミング実践編
ユーザー simansiman
提出日時 2023-01-26 20:23:20
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 367 ms / 2,000 ms
コード長 2,247 bytes
コンパイル時間 4,068 ms
コンパイル使用メモリ 88,816 KB
実行使用メモリ 29,496 KB
最終ジャッジ日時 2023-09-09 19:15:53
合計ジャッジ時間 10,842 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 4 ms
4,380 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 4 ms
4,380 KB
testcase_22 AC 4 ms
4,380 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 349 ms
29,452 KB
testcase_25 AC 337 ms
29,388 KB
testcase_26 AC 338 ms
29,420 KB
testcase_27 AC 340 ms
29,496 KB
testcase_28 AC 334 ms
29,460 KB
testcase_29 AC 340 ms
29,472 KB
testcase_30 AC 334 ms
29,464 KB
testcase_31 AC 333 ms
29,372 KB
testcase_32 AC 332 ms
29,448 KB
testcase_33 AC 367 ms
29,440 KB
testcase_34 AC 115 ms
25,988 KB
testcase_35 AC 3 ms
4,384 KB
testcase_36 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <stack>
#include <string.h>
#include <vector>
#include <climits>
using namespace std;

typedef vector <vector<int>> Graph;

vector<int> tsort(Graph G, int start, int end) {
  vector<int> ret;

  int degree[end];
  memset(degree, 0, sizeof(degree));

  for (int from = start; from < end; from++) {
    for (int i = 0; i < G[from].size(); i++) {
      int to = G[from][i];
      degree[to]++;
    }
  }

  stack<int> root;

  for (int i = start; i < end; i++) {
    if (degree[i] == 0) {
      root.push(i);
    }
  }

  while (!root.empty()) {
    int from = root.top();
    root.pop();

    ret.push_back(from);

    for (int i = 0; i < G[from].size(); i++) {
      int to = G[from][i];
      degree[to]--;

      if (degree[to] == 0) {
        root.push(to);
      }
    }
  }

  return ret;
}

struct Edge {
  int to;
  int cost;

  Edge(int to = -1, int cost = -1) {
    this->to = to;
    this->cost = cost;
  }
};

int main() {
  int N, M;
  cin >> N >> M;

  Graph G(N);

  vector<Edge> edges[N];
  vector<Edge> reverse_edges[N];

  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b >> c;

    G[a].push_back(b);
    edges[a].push_back(Edge(b, c));
    reverse_edges[b].push_back(Edge(a, c));
  }

  vector<int> res = tsort(G, 0, N);

  if (res.size() != N) {
    // tsort failed.
    cout << -1 << endl;
  }

  int dp[N];
  memset(dp, 0, sizeof(dp));
  int max_cost = 0;

  for (int i = 0; i < res.size(); ++i) {
    int v = res[i];

    for (Edge &e : edges[v]) {
      int n_cost = dp[v] + e.cost;
      dp[e.to] = max(dp[e.to], n_cost);
      max_cost = max(max_cost, dp[e.to]);
    }
  }

  int min_cost[N];
  for (int v = 0; v < N; ++v) {
    min_cost[v] = INT_MAX;
  }

  for (int i = N - 1; i >= 0; --i) {
    int v = res[i];
    if (min_cost[v] == INT_MAX) {
      min_cost[v] = dp[v];
    }
    
    for (Edge &e : reverse_edges[v]) {
      int b_cost = min_cost[v] - e.cost;
      min_cost[e.to] = min(min_cost[e.to], b_cost);
    }
  }

  int buf_size = 0;
  for (int v = 0; v < N; ++v) {
    // fprintf(stderr, "v: %d, dp: %d, min: %d\n", v, dp[v], min_cost[v]);
    if (dp[v] < min_cost[v]) ++buf_size;
  }

  cout << max_cost << " " << buf_size << "/" << N << endl;

  return 0;
}

0