QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#460763#3304. Interval Graphalpha1022TL 91ms106664kbC++143.0kb2024-07-02 08:12:472024-07-02 08:12:48

Judging History

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

  • [2024-07-02 08:12:48]
  • 评测
  • 测评结果:TL
  • 用时:91ms
  • 内存:106664kb
  • [2024-07-02 08:12:47]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

template<class Z, class ZC>
struct CostFlow {
  static constexpr Z inf = numeric_limits<Z>::max();
  static constexpr ZC cinf = numeric_limits<ZC>::max();

  int n;
  vector<vector<tuple<int, int, Z, ZC>>> g;

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

  int newNode() { g.emplace_back(); return 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, cinf);
      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] == cinf) return {0, 0};
    auto getLevel = [&]() {
      fill_n(dis.begin(), n, cinf);
      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);
      }
    };
    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, c -= flow, get<2>(g[v][rev]) += flow;
          if (ret == lim) return ret;
        }
      }
      return ret;
    };
    Z ret = 0; ZC cost = 0;
    while (getLevel(), level[t] != -1) {
      for (int i = 0; i < n; ++i) cur[i] = g[i].size();
      Z flow = cap(s, inf); ret += flow, cost += (dis[t] + h[t]) * flow;
      for (int i = 0; i < n; ++i) h[i] += dis[i];
    }
    return {ret, cost};
  }
};

int main() {
  int n = 5e5, m, k = 2; scanf("%d", &m);
  CostFlow<int, ll> costFlow(n + 2); int s = n, t = n + 1;
  for (int i = 1; i < n; ++i) costFlow.addEdge(i - 1, i, costFlow.inf, 0);
  costFlow.addEdge(s, 0, k, 0), costFlow.addEdge(n - 1, t, k, 0);
  for (int i = 1; i <= m; ++i) {
    int l, r, a; scanf("%d%d%d", &l, &r, &a), costFlow.addEdge(l - 1, r - 1, 1, -a);
  }
  printf("%lld\n", -costFlow.dinic(s, t).second);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 91ms
memory: 106664kb

input:

3
1 3 10
3 5 20
5 7 30

output:

60

result:

ok single line: '60'

Test #2:

score: -100
Time Limit Exceeded

input:

3
1 3 1
2 3 2
3 3 3

output:


result: