結果

問題 No.468 役に立つ競技プログラミング実践編
コンテスト
ユーザー yuppe19 😺
提出日時 2016-12-26 00:02:37
言語 C++11(old_compat)
(gcc 12.4.0 + boost 1.89.0)
コンパイル:
g++-12 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -include bits/stdc++.h -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 187 ms / 2,000 ms
コード長 1,546 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,440 ms
コンパイル使用メモリ 177,600 KB
実行使用メモリ 26,004 KB
最終ジャッジ日時 2026-03-08 16:08:15
合計ジャッジ時間 4,783 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 31
other AC * 6
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:32:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   32 |   int n, m; scanf("%d%d", &n, &m);
      |             ~~~~~^~~~~~~~~~~~~~~~
main.cpp:36:27: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   36 |     int a, b; i64 c; scanf("%d%d%lld", &a, &b, &c);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #
raw source code

#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;
using i64 = long long;

struct edge { int to; i64 cost; };

bool visit(const vector<vector<edge>> &G, int v, vector<int> &order, vector<int> &color) {
  color[v] = 1;
  for(edge e : G[v]) {
    if(color[e.to] == 2) { continue; }
    if(color[e.to] == 1) { return false; }
    if(!visit(G, e.to, order, color)) { return false; }
  }
  order.push_back(v);
  color[v] = 2;
  return true;
}

bool topological_sort(const vector<vector<edge>> &G, vector<int> &order) {
  int n = G.size();
  vector<int> color(n);
  for(int u=0; u<n; ++u) {
    if(!color[u] && !visit(G, u, order, color)) { return false; }
  }
  reverse(begin(order), end(order));
  return true;
}

int main(void) {
  int n, m; scanf("%d%d", &n, &m);
  vector<vector<edge>> fG(n, vector<edge>()),
                       rG(n, vector<edge>());
  for(int i=0; i<m; ++i) {
    int a, b; i64 c; scanf("%d%d%lld", &a, &b, &c);
    fG[a].push_back(edge({b, c}));
    rG[b].push_back(edge({a, c}));
  }
  vector<int> order;
  bool ok = topological_sort(fG, order);
  assert(ok);

  vector<i64> dp0(n), dp1(n);
  for(int i=0; i<n; ++i) {
    for(edge &e : fG[order[i]]) {
      dp0[e.to] = max(dp0[e.to], dp0[order[i]] + e.cost);
    }
    for(edge &e : rG[order[n-i-1]]) {
      dp1[e.to] = min(dp1[e.to], dp1[order[n-i-1]] - e.cost);
    }
  }

  i64 days = dp0[n-1];
  int cnt = 0;
  for(int i=0; i<n; ++i) {
    if(dp0[i] != days + dp1[i]) { ++cnt; }
  }
  printf("%lld %d/%d\n", days, cnt, n);
  return 0;
}
0