QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#306167#7185. Poor StudentsckisekiRE 0ms3776kbC++234.8kb2024-01-16 14:46:082024-01-16 14:46:08

Judging History

你现在查看的是最新测评结果

  • [2024-01-16 14:46:08]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3776kb
  • [2024-01-16 14:46:08]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define all(x) begin(x), end(x)
#ifdef local
#include <experimental/iterator>
#define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(const char *s, auto ...a) {
  cerr << "\e[1;32m(" << s << ") = (";
  int f = 0;
  (..., (cerr << (f++ ? ", " : "") << a));
  cerr << ")\e[0m\n";
}
void orange_(const char *s, auto L, auto R) {
  cerr << "\e[1;33m[ " << s << " ] = [ ";
  using namespace experimental;
  copy(L, R, make_ostream_joiner(cerr, ", "));
  cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif

using lld = int64_t;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;

template <typename T>
bool chmin(T &x, T v) {
  if (x > v) return x = v, true;
  return false;
}

// const int N = 105;
// const int M = 1525;

const int maxn = 105;

template <typename F, typename C>
struct MinCostCirculation {
  struct ep { int to; F flow; C cost; };
  int n; vector<int> vis; int visc;
  vector<int> fa, fae; vector<vector<int>> g;
  vector<ep> e; vector<C> pi;
  MinCostCirculation(int n_) : n(n_), vis(n), visc(0), g(n), pi(n) {}
  void add_edge(int u, int v, F fl, C cs) {
    g[u].emplace_back((int)e.size());
    e.emplace_back(v, fl, cs);
    g[v].emplace_back((int)e.size());
    e.emplace_back(u, 0, -cs);
  }
  C phi(int x) {
    if (fa[x] == -1) return 0;
    if (vis[x] == visc) return pi[x];
    return vis[x] = visc, pi[x] = phi(fa[x]) - e[fae[x]].cost;
  }
  int lca(int u, int v) {
    for (; u != -1 || v != -1; swap(u, v)) if (u != -1) {
      if (vis[u] == visc) return u;
      vis[u] = visc;
      u = fa[u];
    }
    return -1;
  }
  void pushflow(int x, F &flow, C &cost) {
    int v = e[x ^ 1].to, u = e[x].to;
    ++visc;
    if (int w = lca(u, v); w == -1) {
      while (v != -1)
        swap(x ^= 1, fae[v]), swap(u, fa[v]), swap(u, v);
    } else {
      int z = u, dir = 0; F f = e[x].flow;
      vector<int> cyc = {x};
      for (int d : {0, 1})
        for (int i = (d ? u : v); i != w; i = fa[i]) {
          cyc.push_back(fae[i] ^ d);
          if (chmin(f, e[fae[i] ^ d].flow)) z = i, dir = d;
        }
      for (int i : cyc) {
        e[i].flow -= f; e[i ^ 1].flow += f;
        cost += f * e[i].cost;
      }
      flow += f;
      if (dir) x ^= 1, swap(u, v);
      while (u != z)
        swap(x ^= 1, fae[v]), swap(u, fa[v]), swap(u, v);
    }
  }
  void dfs(int u) {
    vis[u] = visc;
    for (int i : g[u])
      if (int v = e[i].to; vis[v] != visc and e[i].flow)
        fa[v] = u, fae[v] = i, dfs(v);
  }
  pair<F, C> simplex() {
    F flow = 0; C cost = 0;
    fa.assign(g.size(), -1); fae.assign(e.size(), -1);
    ++visc; dfs(0);
    for (int fail = 0; fail < ssize(e); )
      for (int i = 0; i < ssize(e); i++)
        if (e[i].flow and e[i].cost < phi(e[i ^ 1].to) - phi(e[i].to))
          fail = 0, pushflow(i, flow, cost), ++visc;
        else ++fail;
    return {flow, cost};
  }

  vector<F> get_cap() {
    vector<F> res;
    for (int i = 0; i < ssize(e); i += 2)
      res.push_back(e[i ^ 1].flow);
    return res;
  }
  vector<C> get_potential() {
    vector<C> d(n);
    vector<int> inq(n, true);
    queue<int> q;
    for (int i = 0; i < n; i++)
      q.push(i);

    while (!q.empty()) {
      int u = q.front(); q.pop();
      inq[u] = false;
      for (int i : g[u]) if (e[i].flow) {
        int v = e[i].to;
        if (d[v] > d[u] + e[i].cost) {
          d[v] = d[u] + e[i].cost;
          if (!inq[v]) {
            inq[v] = true;
            q.push(v);
          }
        }
      }
    }
    return d;
  }
};

using i128 = __int128;
using lld = int64_t;

namespace std {
  ostream &operator<<(ostream &os, const i128 &x) {
    if (x < 0) return os << '-' << -x;
    if (x < 10) return os << int(x % 10);
    return os << x / 10 << int(x % 10);
  }
}

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  auto c = vector(n, vector(k, 0));
  auto a = vector(k, 0);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < k; j++)
      cin >> c[i][j];
  for (int i = 0; i < k; i++)
    cin >> a[i];

  const int S = n + k, T = n + k + 1, N = T + 1;
  MinCostCirculation<lld, i128> flow(N);

  for (int i = 0; i < k; i++) {
    flow.add_edge(S, i, a[i], 0);
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < k; j++)
      flow.add_edge(j, i + k, 1, c[i][j]);
  for (int i = 0; i < n; i++)
    flow.add_edge(i + k, T, 1, 0);

  lld LARGE = 1e18;
  flow.add_edge(T, S, LARGE, -LARGE);

  auto [f, cost] = flow.simplex();
  assert(f == n);
  cout << cost + i128(LARGE) * f << '\n';
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3776kb

input:

6 2
1 2
1 3
1 4
1 5
1 6
1 7
3 4

output:

12

result:

ok answer is '12'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3596kb

input:

3 3
1 2 3
2 4 6
6 5 4
1 1 1

output:

8

result:

ok answer is '8'

Test #3:

score: -100
Runtime Error

input:

1000 10
734 303 991 681 755 155 300 483 702 442
237 256 299 675 671 757 112 853 759 233
979 340 288 377 718 199 935 666 576 842
537 363 592 349 494 961 864 727 84 813
340 78 600 492 118 421 478 925 552 617
517 589 716 7 928 638 258 297 706 787
266 746 913 978 436 859 701 951 137 44
815 336 471 720 2...

output:


result: