結果

問題 No.2604 Initial Motion
ユーザー MisukiMisuki
提出日時 2024-01-12 22:01:12
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 943 ms / 3,000 ms
コード長 4,909 bytes
コンパイル時間 2,841 ms
コンパイル使用メモリ 213,304 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2024-01-12 22:01:37
合計ジャッジ時間 19,790 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 25 ms
6,548 KB
testcase_04 AC 25 ms
6,548 KB
testcase_05 AC 25 ms
6,548 KB
testcase_06 AC 25 ms
6,548 KB
testcase_07 AC 26 ms
6,548 KB
testcase_08 AC 25 ms
6,548 KB
testcase_09 AC 25 ms
6,548 KB
testcase_10 AC 25 ms
6,548 KB
testcase_11 AC 26 ms
6,548 KB
testcase_12 AC 26 ms
6,548 KB
testcase_13 AC 781 ms
6,548 KB
testcase_14 AC 527 ms
6,548 KB
testcase_15 AC 309 ms
6,548 KB
testcase_16 AC 679 ms
6,548 KB
testcase_17 AC 943 ms
6,548 KB
testcase_18 AC 879 ms
6,548 KB
testcase_19 AC 827 ms
6,548 KB
testcase_20 AC 689 ms
6,548 KB
testcase_21 AC 545 ms
6,548 KB
testcase_22 AC 849 ms
6,548 KB
testcase_23 AC 574 ms
6,548 KB
testcase_24 AC 756 ms
6,548 KB
testcase_25 AC 865 ms
6,548 KB
testcase_26 AC 682 ms
6,548 KB
testcase_27 AC 461 ms
6,548 KB
testcase_28 AC 591 ms
6,548 KB
testcase_29 AC 742 ms
6,548 KB
testcase_30 AC 504 ms
6,548 KB
testcase_31 AC 630 ms
6,548 KB
testcase_32 AC 583 ms
6,548 KB
testcase_33 AC 219 ms
6,548 KB
testcase_34 AC 327 ms
6,548 KB
testcase_35 AC 360 ms
6,548 KB
testcase_36 AC 324 ms
6,548 KB
testcase_37 AC 171 ms
6,548 KB
testcase_38 AC 3 ms
6,548 KB
testcase_39 AC 3 ms
6,548 KB
testcase_40 AC 264 ms
6,548 KB
testcase_41 AC 270 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O2")
#include <algorithm>
#include <array>
#include <bit>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <compare>
#include <complex>
#include <concepts>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numbers>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ranges>
#include <set>
#include <span>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>

#define int ll
#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)

#ifdef DEBUG
#define dbg(x) cout << (#x) << " = " << x << '\n'
#else
#define dbg(x)
#endif

namespace R = std::ranges;
namespace V = std::views;

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
//#define double ldb

template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
  for(const T &X : arr)
    os << X << ' ';
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
  for(const T &X : vec)
    os << X << ' ';
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const set<T> &s) {
  for(const T &x : s)
    os << x << ' ';
  return os;
}

/**
 * template name: MCMF
 * author: Misuki
 * last update: 2021/11/11
 */

struct MCMF {
  struct Edge {
    int to, rev;
    long long cap, cos;
    Edge(int _to, long long _cap, long long _cos, int _rev) :
        to(_to), cap(_cap), cos(_cos), rev(_rev) {}
  };

  static const int SIZE = 2002;
  int n, s, t;
  vector<Edge> G[SIZE];
  array<int, SIZE> par, idx;
  array<long long, SIZE> pot, dis, f; 

  void init(int _n, int _s, int _t) {
    n = _n, s = _s, t = _t;
    for(int i = 0; i < n; i++)
      G[i].clear();
  }

  void addEdge(int from, int to, long long cap, long long cos) {
    G[from].emplace_back(Edge(to, cap, cos, G[to].size()));
    G[to].emplace_back(Edge(from, 0, -cos, (int)G[from].size() - 1));
  }

  void initPotential() {
    fill(dis.begin(), dis.end(), LLONG_MAX);
    dis[s] = 0;
    for(int i = 1; i < n; i++) {
      for(int j = 0; j < n; j++) {
        if (dis[j] == LLONG_MAX)
          continue;
        for(Edge E : G[j]) {
          if (E.cap == 0)
            continue;
          if (dis[j] + E.cos < dis[E.to])
            dis[E.to] = dis[j] + E.cos;
        }
      }
    }
    pot.swap(dis);
  }

  pll flow() {
    long long Cost = 0, Flow = 0;
    while(true) {
      priority_queue<pii, vector<pii>, greater<pii> > pq;
      fill(dis.begin(), dis.end(), LLONG_MAX);
      dis[s] = 0, f[s] = LLONG_MAX;
      pq.push(make_pair(0, s));
      while(!pq.empty()) {
        pii now = pq.top(); pq.pop();
        if (dis[now.second] != now.first)
          continue;

        int V = now.second;
        for(Edge E : G[V]) {
          if (E.cap == 0)
            continue;
          if (dis[V] + E.cos + pot[V] - pot[E.to] < dis[E.to]) {
            dis[E.to] = dis[V] + E.cos + pot[V] - pot[E.to];
            f[E.to] = min(f[V], E.cap);
            par[E.to] = V;
            idx[E.to] = G[E.to][E.rev].rev;
            pq.push(make_pair(dis[E.to], E.to));
          }
        }
      }

      if (dis[t] == LLONG_MAX)
        break;

      long long bot = f[t];
      int now = t;
      while(now != s) {
        Edge &E = G[par[now]][idx[now]];
        E.cap -= bot;
        G[now][E.rev].cap += bot;
        now = par[now];
      }
      Flow += bot, Cost += bot * (dis[t] - pot[s] + pot[t]);

      for(int i = 0; i < n; i++) {
        dis[i] += pot[i] - pot[s];
      }
      pot.swap(dis);
    }

    return make_pair(Flow, Cost);
  }
};

signed main() {
  ios::sync_with_stdio(false), cin.tie(NULL);

  int k, n, m; cin >> k >> n >> m;
  vector<int> a(k), b(n);
  for(int &x : a) {
    cin >> x;
    x--;
  }
  for(int &x : b)
    cin >> x;
  vector<array<int, 3>> e(m);
  for(auto &[u, v, w] : e) {
    cin >> u >> v >> w;
    u--, v--;
  }

  const int S = n, T = n + 1;
  MCMF flow;
  flow.init(n + 2, S, T);
  for(auto [u, v, w] : e) {
    flow.addEdge(u, v, INT_MAX, w);
    flow.addEdge(v, u, INT_MAX, w);
  }
  for(int x : a)
    flow.addEdge(S, x, 1, 0);
  for(int i = 0; i < n; i++)
    flow.addEdge(i, T, b[i], 0);

  flow.initPotential();
  cout << flow.flow().second << '\n';

  return 0;
}
0