QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#399732#2208. Flowhos_lyricAC ✓33ms12292kbC++145.3kb2024-04-26 17:26:142024-04-26 17:26:14

Judging History

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

  • [2024-04-26 17:26:14]
  • 评测
  • 测评结果:AC
  • 用时:33ms
  • 内存:12292kb
  • [2024-04-26 17:26:14]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


using Int = long long;

namespace MCF {

using Capa = int;
using Cost = Int;
constexpr int MAX_N = 2'010;
constexpr int MAX_M = 1'002'010;
constexpr int QUE_SIZE = 1 << (32 - __builtin_clz(MAX_N));
constexpr int BELLMAN_FORD_NUM_ITERS = 10;
constexpr int LOG_SCALING = 2;

int n, m, ptr[MAX_N], cur[MAX_N], next[MAX_M << 1], zu[MAX_M << 1];
bool on[MAX_N];
int que[QUE_SIZE], qb, qe;
Capa capa[MAX_M << 1], ex[MAX_N];
Cost cost[MAX_M << 1], pot[MAX_N], pot0[MAX_N];

void init(int n_) {
  n = n_; m = 0; memset(ptr, ~0, n * sizeof(int)); memset(ex, 0, n * sizeof(Capa));
}
void ae(int u, int v, Capa c, Cost d) {
  d *= (n + 1);
  next[m] = ptr[u]; ptr[u] = m; zu[m] = v; capa[m] = c; cost[m] = d; ++m;
  next[m] = ptr[v]; ptr[v] = m; zu[m] = u; capa[m] = 0; cost[m] = -d; ++m;
}

bool bellmanFord(Cost eps) {
  memcpy(pot0, pot, n * sizeof(Cost));
  for (int iter = 0; iter < BELLMAN_FORD_NUM_ITERS; ++iter) {
    bool upd = false;
    for (int i = 0; i < m; ++i) {
      if (capa[i] > 0) {
        const int u = zu[i ^ 1], v = zu[i];
        if (pot0[v] > pot0[u] + cost[i] + eps) {
          pot0[v] = pot0[u] + cost[i] + eps;
          upd = true;
        }
      }
    }
    if (!upd) {
      memcpy(pot, pot0, n * sizeof(Cost));
      return true;
    }
  }
  return false;
}

Cost solve() {
  Cost minCost = 0;
  for (int i = 0; i < m; i += 2) if (minCost > cost[i]) minCost = cost[i];
  Cost eps = 1;
  for (; eps < -minCost; eps <<= LOG_SCALING) {}
  memset(pot, 0, n * sizeof(Cost));
  for (; eps >>= LOG_SCALING; ) {
    if (bellmanFord(eps)) continue;
    for (int i = 0; i < m; i += 2) {
      const int u = zu[i ^ 1], v = zu[i];
      const Cost d = cost[i] + pot[u] - pot[v];
      if (capa[i] > 0 && d < 0) {
        Capa &c = capa[i]; ex[u] -= c; ex[v] += c; capa[i ^ 1] += c; c = 0;
      } else if (capa[i ^ 1] > 0 && -d < 0) {
        Capa &c = capa[i ^ 1]; ex[v] -= c; ex[u] += c; capa[i] += c; c = 0;
      }
    }
    memcpy(cur, ptr, n * sizeof(int));
    qb = qe = 0;
    for (int u = 0; u < n; ++u) if (ex[u] > 0) {
      que[qe] = u; ++qe &= QUE_SIZE - 1;
    }
    for (; qb != qe; ) {
      const int u = que[qb]; ++qb &= QUE_SIZE - 1;
      for (int &i = cur[u]; ~i; i = next[i]) {
        if (capa[i] > 0) {
          const int v = zu[i];
          if (cost[i] + pot[u] - pot[v] < 0) {
            const Capa c = min(ex[u], capa[i]);
            if (ex[v] <= 0 && ex[v] + c > 0) {
              que[qe] = v; ++qe &= QUE_SIZE - 1;
            }
            ex[u] -= c; ex[v] += c; capa[i] -= c; capa[i ^ 1] += c;
            if (ex[u] == 0) break;
          }
        }
      }
      if (ex[u] > 0) {
        bool relabeled = false;
        for (int i = ptr[u]; ~i; i = next[i]) {
          if (capa[i] > 0) {
            const Cost p = pot[zu[i]] - cost[i] - eps;
            if (!relabeled || pot[u] < p) {
              relabeled = true; pot[u] = p;
            }
          }
        }
        cur[u] = ptr[u]; que[qe] = u; ++qe &= QUE_SIZE - 1;
      }
    }
  }
  Cost totalCost = 0;
  for (int i = 0; i < m; i += 2) totalCost += cost[i] * capa[i ^ 1];
  return totalCost / (n + 1);
}

}  // namespace MCF


/*
  f[k]: max flow in the problem graph
  C: max score circulation (input u v w: u -> v, capa w, score 1)
  
  decompose C into cycles
  l-cycle, flow f: can have flow f for l cyclic shifts (+= l f)
  therefore f[k] >= C
  
  augmenting path in the problem graph
  walk in residual network for C
    score +1: layer id += 1
    score -1: layer id -= 1
  if k >= N, \exists cycle, layer id increases
  ==> positive cycle (impossible)
*/

int N, M;
vector<int> U, V, W;

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    U.resize(M);
    V.resize(M);
    W.resize(M);
    for (int i = 0; i < M; ++i) {
      scanf("%d%d%d", &U[i], &V[i], &W[i]);
      --U[i];
      --V[i];
    }
    
    MCF::init(N);
    for (int i = 0; i < M; ++i) {
      MCF::ae(U[i], V[i], W[i], -1);
    }
    const Int ans = -MCF::solve();
    printf("%lld\n", ans);
  }
  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 16ms
memory: 10172kb

Test #2:

score: 0
Accepted
time: 5ms
memory: 10056kb

Test #3:

score: 0
Accepted
time: 9ms
memory: 10236kb

Test #4:

score: 0
Accepted
time: 11ms
memory: 10232kb

Test #5:

score: 0
Accepted
time: 17ms
memory: 10180kb

Test #6:

score: 0
Accepted
time: 12ms
memory: 10188kb

Test #7:

score: 0
Accepted
time: 22ms
memory: 10060kb

Test #8:

score: 0
Accepted
time: 6ms
memory: 10176kb

Test #9:

score: 0
Accepted
time: 12ms
memory: 10192kb

Test #10:

score: 0
Accepted
time: 15ms
memory: 10176kb

Test #11:

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

Test #12:

score: 0
Accepted
time: 22ms
memory: 10188kb

Test #13:

score: 0
Accepted
time: 20ms
memory: 12068kb

Test #14:

score: 0
Accepted
time: 22ms
memory: 10200kb

Test #15:

score: 0
Accepted
time: 4ms
memory: 10088kb

Test #16:

score: 0
Accepted
time: 7ms
memory: 10192kb

Test #17:

score: 0
Accepted
time: 9ms
memory: 10188kb

Test #18:

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

Test #19:

score: 0
Accepted
time: 19ms
memory: 10188kb

Test #20:

score: 0
Accepted
time: 2ms
memory: 10152kb

Test #21:

score: 0
Accepted
time: 8ms
memory: 10184kb

Test #22:

score: 0
Accepted
time: 7ms
memory: 10176kb

Test #23:

score: 0
Accepted
time: 18ms
memory: 10192kb

Test #24:

score: 0
Accepted
time: 10ms
memory: 10184kb

Test #25:

score: 0
Accepted
time: 26ms
memory: 10060kb

Test #26:

score: 0
Accepted
time: 14ms
memory: 10092kb

Test #27:

score: 0
Accepted
time: 5ms
memory: 10040kb

Test #28:

score: 0
Accepted
time: 7ms
memory: 10168kb

Test #29:

score: 0
Accepted
time: 20ms
memory: 10188kb

Test #30:

score: 0
Accepted
time: 26ms
memory: 10192kb

Test #31:

score: 0
Accepted
time: 8ms
memory: 10240kb

Test #32:

score: 0
Accepted
time: 25ms
memory: 10196kb

Test #33:

score: 0
Accepted
time: 16ms
memory: 10180kb

Test #34:

score: 0
Accepted
time: 23ms
memory: 10248kb

Test #35:

score: 0
Accepted
time: 12ms
memory: 10192kb

Test #36:

score: 0
Accepted
time: 21ms
memory: 10196kb

Test #37:

score: 0
Accepted
time: 26ms
memory: 12244kb

Test #38:

score: 0
Accepted
time: 19ms
memory: 10068kb

Test #39:

score: 0
Accepted
time: 23ms
memory: 10104kb

Test #40:

score: 0
Accepted
time: 20ms
memory: 10196kb

Test #41:

score: 0
Accepted
time: 3ms
memory: 10160kb

Test #42:

score: 0
Accepted
time: 8ms
memory: 10188kb

Test #43:

score: 0
Accepted
time: 14ms
memory: 12140kb

Test #44:

score: 0
Accepted
time: 15ms
memory: 10076kb

Test #45:

score: 0
Accepted
time: 21ms
memory: 10176kb

Test #46:

score: 0
Accepted
time: 19ms
memory: 10184kb

Test #47:

score: 0
Accepted
time: 12ms
memory: 10232kb

Test #48:

score: 0
Accepted
time: 18ms
memory: 10140kb

Test #49:

score: 0
Accepted
time: 10ms
memory: 10184kb

Test #50:

score: 0
Accepted
time: 17ms
memory: 10176kb

Test #51:

score: 0
Accepted
time: 4ms
memory: 10096kb

Test #52:

score: 0
Accepted
time: 4ms
memory: 10032kb

Test #53:

score: 0
Accepted
time: 3ms
memory: 10220kb

Test #54:

score: 0
Accepted
time: 20ms
memory: 10032kb

Test #55:

score: 0
Accepted
time: 3ms
memory: 10040kb

Test #56:

score: 0
Accepted
time: 17ms
memory: 10060kb

Test #57:

score: 0
Accepted
time: 7ms
memory: 10088kb

Test #58:

score: 0
Accepted
time: 19ms
memory: 12168kb

Test #59:

score: 0
Accepted
time: 20ms
memory: 10056kb

Test #60:

score: 0
Accepted
time: 22ms
memory: 10184kb

Test #61:

score: 0
Accepted
time: 20ms
memory: 10196kb

Test #62:

score: 0
Accepted
time: 7ms
memory: 10196kb

Test #63:

score: 0
Accepted
time: 10ms
memory: 10232kb

Test #64:

score: 0
Accepted
time: 4ms
memory: 12128kb

Test #65:

score: 0
Accepted
time: 14ms
memory: 10176kb

Test #66:

score: 0
Accepted
time: 18ms
memory: 10116kb

Test #67:

score: 0
Accepted
time: 6ms
memory: 10040kb

Test #68:

score: 0
Accepted
time: 20ms
memory: 12020kb

Test #69:

score: 0
Accepted
time: 9ms
memory: 10176kb

Test #70:

score: 0
Accepted
time: 8ms
memory: 10104kb

Test #71:

score: 0
Accepted
time: 2ms
memory: 10116kb

Test #72:

score: 0
Accepted
time: 6ms
memory: 12200kb

Test #73:

score: 0
Accepted
time: 7ms
memory: 12224kb

Test #74:

score: 0
Accepted
time: 15ms
memory: 10056kb

Test #75:

score: 0
Accepted
time: 7ms
memory: 10124kb

Test #76:

score: 0
Accepted
time: 12ms
memory: 10188kb

Test #77:

score: 0
Accepted
time: 15ms
memory: 10176kb

Test #78:

score: 0
Accepted
time: 28ms
memory: 10184kb

Test #79:

score: 0
Accepted
time: 21ms
memory: 10200kb

Test #80:

score: 0
Accepted
time: 15ms
memory: 10192kb

Test #81:

score: 0
Accepted
time: 10ms
memory: 10088kb

Test #82:

score: 0
Accepted
time: 33ms
memory: 10188kb

Test #83:

score: 0
Accepted
time: 19ms
memory: 10096kb

Test #84:

score: 0
Accepted
time: 10ms
memory: 10204kb

Test #85:

score: 0
Accepted
time: 23ms
memory: 10192kb

Test #86:

score: 0
Accepted
time: 18ms
memory: 12280kb

Test #87:

score: 0
Accepted
time: 3ms
memory: 10164kb

Test #88:

score: 0
Accepted
time: 6ms
memory: 10188kb

Test #89:

score: 0
Accepted
time: 3ms
memory: 10164kb

Test #90:

score: 0
Accepted
time: 24ms
memory: 10196kb

Test #91:

score: 0
Accepted
time: 3ms
memory: 10092kb

Test #92:

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

Test #93:

score: 0
Accepted
time: 11ms
memory: 10132kb

Test #94:

score: 0
Accepted
time: 24ms
memory: 10140kb

Test #95:

score: 0
Accepted
time: 10ms
memory: 10188kb

Test #96:

score: 0
Accepted
time: 4ms
memory: 10224kb

Test #97:

score: 0
Accepted
time: 12ms
memory: 10132kb

Test #98:

score: 0
Accepted
time: 21ms
memory: 10136kb

Test #99:

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

Test #100:

score: 0
Accepted
time: 24ms
memory: 10140kb

Test #101:

score: 0
Accepted
time: 20ms
memory: 12292kb

Test #102:

score: 0
Accepted
time: 27ms
memory: 10184kb

Test #103:

score: 0
Accepted
time: 5ms
memory: 10100kb

Test #104:

score: 0
Accepted
time: 26ms
memory: 10140kb

Test #105:

score: 0
Accepted
time: 13ms
memory: 12260kb

Test #106:

score: 0
Accepted
time: 17ms
memory: 10060kb

Test #107:

score: 0
Accepted
time: 5ms
memory: 10108kb

Test #108:

score: 0
Accepted
time: 11ms
memory: 10048kb

Test #109:

score: 0
Accepted
time: 8ms
memory: 10048kb

Test #110:

score: 0
Accepted
time: 10ms
memory: 10136kb

Test #111:

score: 0
Accepted
time: 7ms
memory: 10176kb

Test #112:

score: 0
Accepted
time: 4ms
memory: 10112kb

Test #113:

score: 0
Accepted
time: 14ms
memory: 10124kb

Test #114:

score: 0
Accepted
time: 19ms
memory: 10192kb

Test #115:

score: 0
Accepted
time: 17ms
memory: 10192kb

Test #116:

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

Test #117:

score: 0
Accepted
time: 11ms
memory: 10048kb

Test #118:

score: 0
Accepted
time: 4ms
memory: 12072kb

Test #119:

score: 0
Accepted
time: 10ms
memory: 10248kb

Test #120:

score: 0
Accepted
time: 16ms
memory: 10200kb

Test #121:

score: 0
Accepted
time: 19ms
memory: 12180kb

Test #122:

score: 0
Accepted
time: 18ms
memory: 10180kb

Test #123:

score: 0
Accepted
time: 13ms
memory: 10188kb

Test #124:

score: 0
Accepted
time: 21ms
memory: 10120kb

Test #125:

score: 0
Accepted
time: 21ms
memory: 10192kb

Test #126:

score: 0
Accepted
time: 14ms
memory: 10172kb

Test #127:

score: 0
Accepted
time: 2ms
memory: 10152kb

Test #128:

score: 0
Accepted
time: 15ms
memory: 10100kb

Test #129:

score: 0
Accepted
time: 17ms
memory: 10140kb

Test #130:

score: 0
Accepted
time: 3ms
memory: 10172kb

Test #131:

score: 0
Accepted
time: 10ms
memory: 10180kb

Test #132:

score: 0
Accepted
time: 6ms
memory: 10080kb

Test #133:

score: 0
Accepted
time: 22ms
memory: 8192kb

Test #134:

score: 0
Accepted
time: 17ms
memory: 10240kb

Test #135:

score: 0
Accepted
time: 19ms
memory: 10200kb

Test #136:

score: 0
Accepted
time: 10ms
memory: 12136kb

Test #137:

score: 0
Accepted
time: 7ms
memory: 10116kb

Test #138:

score: 0
Accepted
time: 6ms
memory: 10056kb

Test #139:

score: 0
Accepted
time: 13ms
memory: 10048kb

Test #140:

score: 0
Accepted
time: 19ms
memory: 6168kb

Test #141:

score: 0
Accepted
time: 22ms
memory: 4156kb

Test #142:

score: 0
Accepted
time: 19ms
memory: 4036kb

Test #143:

score: 0
Accepted
time: 22ms
memory: 4168kb

Test #144:

score: 0
Accepted
time: 21ms
memory: 4172kb

Test #145:

score: 0
Accepted
time: 10ms
memory: 4164kb

Test #146:

score: 0
Accepted
time: 10ms
memory: 4208kb

Test #147:

score: 0
Accepted
time: 16ms
memory: 4036kb

Test #148:

score: 0
Accepted
time: 11ms
memory: 10064kb

Test #149:

score: 0
Accepted
time: 19ms
memory: 10188kb

Test #150:

score: 0
Accepted
time: 5ms
memory: 10172kb

Test #151:

score: 0
Accepted
time: 15ms
memory: 10080kb

Test #152:

score: 0
Accepted
time: 10ms
memory: 10188kb

Test #153:

score: 0
Accepted
time: 22ms
memory: 10136kb

Test #154:

score: 0
Accepted
time: 11ms
memory: 10176kb

Test #155:

score: 0
Accepted
time: 22ms
memory: 12188kb

Test #156:

score: 0
Accepted
time: 22ms
memory: 10248kb

Test #157:

score: 0
Accepted
time: 2ms
memory: 10216kb

Test #158:

score: 0
Accepted
time: 17ms
memory: 10184kb

Test #159:

score: 0
Accepted
time: 24ms
memory: 10240kb

Test #160:

score: 0
Accepted
time: 9ms
memory: 10132kb

Test #161:

score: 0
Accepted
time: 5ms
memory: 10180kb

Test #162:

score: 0
Accepted
time: 6ms
memory: 10080kb

Test #163:

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

Test #164:

score: 0
Accepted
time: 16ms
memory: 10188kb

Test #165:

score: 0
Accepted
time: 6ms
memory: 10224kb

Test #166:

score: 0
Accepted
time: 15ms
memory: 10176kb

Test #167:

score: 0
Accepted
time: 14ms
memory: 10064kb

Test #168:

score: 0
Accepted
time: 23ms
memory: 10208kb

Test #169:

score: 0
Accepted
time: 20ms
memory: 10096kb

Test #170:

score: 0
Accepted
time: 9ms
memory: 4172kb

Test #171:

score: 0
Accepted
time: 18ms
memory: 4164kb

Test #172:

score: 0
Accepted
time: 8ms
memory: 10048kb

Test #173:

score: 0
Accepted
time: 23ms
memory: 10200kb

Test #174:

score: 0
Accepted
time: 8ms
memory: 10196kb

Test #175:

score: 0
Accepted
time: 8ms
memory: 4168kb

Test #176:

score: 0
Accepted
time: 19ms
memory: 4100kb

Test #177:

score: 0
Accepted
time: 6ms
memory: 4212kb

Test #178:

score: 0
Accepted
time: 5ms
memory: 4168kb

Test #179:

score: 0
Accepted
time: 10ms
memory: 4160kb

Test #180:

score: 0
Accepted
time: 2ms
memory: 4008kb

Test #181:

score: 0
Accepted
time: 18ms
memory: 4160kb

Test #182:

score: 0
Accepted
time: 2ms
memory: 4208kb

Test #183:

score: 0
Accepted
time: 8ms
memory: 4152kb

Test #184:

score: 0
Accepted
time: 24ms
memory: 4036kb

Test #185:

score: 0
Accepted
time: 31ms
memory: 10144kb

Test #186:

score: 0
Accepted
time: 19ms
memory: 10184kb