QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#397690#2208. Flowalpha1022WA 6ms4048kbC++143.0kb2024-04-24 15:58:362024-04-24 15:58:36

Judging History

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

  • [2024-04-24 15:58:36]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:4048kb
  • [2024-04-24 15:58:36]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

template<class Z, class ZC>
struct CostFlow {
  int n;
  vector<vector<tuple<int, int, Z, ZC>>> g;

  CostFlow(int n) : n(n), g(n) {}

  void addEdge(int u, int v, Z c, ZC w) {
    int ru = g[u].size(), rv = g[v].size();
    g[u].emplace_back(v, rv, c, w), g[v].emplace_back(u, ru, 0, -w);
  }

  pair<Z, ZC> dinic(int s, int t) {
    vector<ZC> h(n), dis(n);
    vector<int> level(n), inQueue(n), cur(n);
    auto getH = [&]() {
      fill_n(h.begin(), n, numeric_limits<ZC>::max());
      queue<int> q; q.push(s), h[s] = 0, inQueue[s] = 1;
      while (!q.empty()) {
        int u = q.front(); q.pop(), inQueue[u] = 0;
        for (auto [v, _, c, w]: g[u]) {
          if (c && h[v] > h[u] + w) {
            h[v] = h[u] + w;
            if (!inQueue[v]) q.push(v), inQueue[v] = 1;
          }
        }
      }
    };
    getH();
    if (h[t] == numeric_limits<ZC>::max()) return {0, 0};
    auto getLevel = [&]() {
      fill_n(dis.begin(), n, numeric_limits<ZC>::max());
      priority_queue<pair<ZC, int>, vector<pair<ZC, int>>, greater<pair<ZC, int>>> pQ;
      pQ.emplace(dis[s] = 0, s);
      while (!pQ.empty()) {
        auto [d, u] = pQ.top(); pQ.pop();
        if (d > dis[u]) continue;
        for (auto [v, _, c, w]: g[u]) {
          w += h[u] - h[v];
          if (c && dis[v] > dis[u] + w) pQ.emplace(dis[v] = dis[u] + w, v);
        }
      }
      queue<int> q; fill_n(level.begin(), n, -1), q.push(s), level[s] = 0;
      while (!q.empty()) {
        int u = q.front(); q.pop();
        for (auto [v, _, c, w]: g[u])
          if (c && dis[v] == dis[u] + w + h[u] - h[v] && level[v] == -1) level[v] = level[u] + 1, q.push(v);
      }
    };
    Z ret = 0; ZC cost = 0;
    function<Z(int, Z)> cap = [&](int u, Z lim) {
      if (u == t) return lim;
      Z ret = 0;
      for (; cur[u] && ret < lim; --cur[u]) {
        auto &[v, rev, c, w] = g[u][cur[u] - 1];
        if (c && dis[v] == dis[u] + w + h[u] - h[v] && level[v] == level[u] + 1) {
          Z flow = cap(v, min(lim - ret, c));
          ret += flow, cost += flow * w, c -= flow, get<2>(g[v][rev]) += flow;
          if (ret == lim) return ret;
        }
      }
      return ret;
    };
    while (getLevel(), level[t] != -1) {
      for (int i = 0; i < n; ++i) cur[i] = g[i].size();
      ret += cap(s, numeric_limits<Z>::max());
      for (int i = 0; i < n; ++i) h[i] += dis[i];
    }
    return {ret, cost};
  }
};

int main() {
  int n, m; scanf("%d%d", &n, &m);
  int ans = 0;
  vector<int> sum(n);
  CostFlow<int, int> costFlow(n + 2);
  int s = n, t = n + 1;
  for (int i = 0; i < m; ++i) {
    int u, v, c; scanf("%d%d%d", &u, &v, &c), --u, --v;
    sum[u] -= c, sum[v] += c, ans += c, costFlow.addEdge(v, u, c, 1);
  }
  for (int i = 0; i < n; ++i)
    if (sum[i] < 0) costFlow.addEdge(s, i, -sum[i], 0);
    else if (sum[i] > 0) costFlow.addEdge(i, t, sum[i], 0);
  printf("%d\n", ans -= costFlow.dinic(s, t).second);
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 6ms
memory: 4048kb