結果

問題 No.798 コレクション
ユーザー koyumeishikoyumeishi
提出日時 2019-03-15 22:37:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 10,708 bytes
コンパイル時間 1,589 ms
コンパイル使用メモリ 122,912 KB
実行使用メモリ 186,120 KB
最終ジャッジ日時 2023-09-14 13:52:40
合計ジャッジ時間 14,675 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,568 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1,515 ms
86,164 KB
testcase_14 TLE -
testcase_15 TLE -
testcase_16 AC 770 ms
56,664 KB
testcase_17 AC 1,597 ms
89,084 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <functional>
#include <set>
#include <ctime>
#include <random>
#include <chrono>
#include <cassert>
#include <tuple>
#include <utility>
using namespace std;

namespace {
  using Integer = long long; //__int128;
  template<class T, class S> istream& operator >> (istream& is, pair<T,S>& p){return is >> p.first >> p.second;}
  template<class T> istream& operator >> (istream& is, vector<T>& vec){for(T& val: vec) is >> val; return is;}
  template<class T> istream& operator ,  (istream& is, T& val){ return is >> val;}
  template<class T, class S> ostream& operator << (ostream& os, const pair<T,S>& p){return os << p.first << " " << p.second;}
  template<class T> ostream& operator << (ostream& os, const vector<T>& vec){for(size_t i=0; i<vec.size(); i++) os << vec[i] << (i==vec.size()-1?"":" "); return os;}
  template<class T> ostream& operator ,  (ostream& os, const T& val){ return os << " " << val;}

  template<class H> void print(const H& head){ cout << head; }
  template<class H, class ... T> void print(const H& head, const T& ... tail){ cout << head << " "; print(tail...); }
  template<class ... T> void println(const T& ... values){ print(values...); cout << endl; }

  template<class H> void eprint(const H& head){ cerr << head; }
  template<class H, class ... T> void eprint(const H& head, const T& ... tail){ cerr << head << " "; eprint(tail...); }
  template<class ... T> void eprintln(const T& ... values){ eprint(values...); cerr << endl; }

  class range{ Integer start_, end_, step_; public: struct range_iterator{ Integer val, step_; range_iterator(Integer v, Integer step) : val(v), step_(step) {} Integer operator * (){return val;} void operator ++ (){val += step_;} bool operator != (range_iterator& x){return step_ > 0 ? val < x.val : val > x.val;} }; range(Integer len) : start_(0), end_(len), step_(1) {} range(Integer start, Integer end) : start_(start), end_(end), step_(1) {} range(Integer start, Integer end, Integer step) : start_(start), end_(end), step_(step) {} range_iterator begin(){ return range_iterator(start_, step_); } range_iterator   end(){ return range_iterator(  end_, step_); } };

  inline string operator "" _s (const char* str, size_t size){ return move(string(str)); }
  constexpr Integer my_pow(Integer x, Integer k, Integer z=1){return k==0 ? z : k==1 ? z*x : (k&1) ? my_pow(x*x,k>>1,z*x) : my_pow(x*x,k>>1,z);}
  constexpr Integer my_pow_mod(Integer x, Integer k, Integer M, Integer z=1){return k==0 ? z%M : k==1 ? z*x%M : (k&1) ? my_pow_mod(x*x%M,k>>1,M,z*x%M) : my_pow_mod(x*x%M,k>>1,M,z);}
  constexpr unsigned long long operator "" _ten (unsigned long long value){ return my_pow(10,value); }

  inline int k_bit(Integer x, int k){return (x>>k)&1;} //0-indexed

  mt19937 mt(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());

  template<class T> string join(const vector<T>& v, const string& sep){ stringstream ss; for(size_t i=0; i<v.size(); i++){ if(i>0) ss << sep; ss << v[i]; } return ss.str(); }

  inline string operator * (string s, int k){ string ret; while(k){ if(k&1) ret += s; s += s; k >>= 1; } return ret; }
}
constexpr long long mod = 9_ten + 7;

// min cost flow 
// https://min-25.hatenablog.com/entry/2017/09/08/182320
template <
  typename CapType, typename TotalCapType, 
  typename CostType, typename TotalCostType
>
class CostScaling {
private:
  static const int alpha = 8; // eps <- max(1, eps / alpha)

  using cap_t = CapType;
  using tcap_t = TotalCapType;
  using cost_t = CostType; // > max{|C|} * (2 * |V|)
  using tcost_t = TotalCostType;
  static constexpr cost_t Inf = (tcap_t(1) << (sizeof(tcap_t) * 8 - 2)) - 1;

  struct InputEdge { int from, to; cap_t b, c; cost_t cost; };
  struct Edge { int to, rev; cap_t cap; cost_t cost; };

  class Dinic {
  public:
    Dinic(int N, const vector<int>& ofs, vector<Edge>& edges, 
        vector<tcap_t>& capacity) 
      : N(N), ofs(ofs), edges(edges), capacity(capacity), last(N) {}

    bool succeeded() {
      // s -> u: capacity[u]
      // u -> t: capacity[u + N]
      tcap_t f = 0;
      for (int u = 0; u < N; ++u) f += capacity[u];
      vector<int> que(N);
      while (f) {
        dist.assign(N, -1);
        int qh = 0, qt = 0, lv = N;
        for (int u = 0; u < N; ++u) if (capacity[u] > 0) que[qt++] = u, dist[u] = 0;
        for (; qh < qt; ) {
          int u = que[qh++];
          if (lv == N && capacity[u + N] > 0) lv = dist[u];
          if (dist[u] > lv) break;
          for (int ei = ofs[u]; ei < ofs[u + 1]; ++ei) {
            int v = edges[ei].to;
            if (edges[ei].cap > 0 && dist[v] == -1) {
              que[qt++] = v, dist[v] = dist[u] + 1;
            }
          }
        }
        if (lv == N) break;
        for (int u = 0; u < N; ++u) last[u] = ofs[u];
        for (int u = 0; u < N; ++u) if (capacity[u] > 0) {
          auto df = block_flow(u, capacity[u]);
          f -= df, capacity[u] -= df;
        }
      }
      return f == 0;
    }

  private:
    tcap_t block_flow(int u, tcap_t f) {
      tcap_t ret = 0;
      if (capacity[u + N] > 0) {
        tcap_t df = min(f, capacity[u + N]);
        capacity[u + N] -= df;
        return df;
      }
      for (auto& ei = last[u]; ei < ofs[u + 1]; ++ei) {
        auto& e = edges[ei]; int v = e.to;
        if (e.cap == 0 || dist[v] <= dist[u]) continue;
        cap_t df = block_flow(v, min<cap_t>(e.cap, f));
        if (df == 0) continue;
        e.cap -= df, edges[e.rev].cap += df;
        f -= df, ret += df;
        if (f == 0) break;
      }
      return ret;
    }

    int N;
    const vector<int>& ofs;
    vector<Edge>& edges;
    vector<tcap_t>& capacity;
    vector<int> last, dist;
  };

public:
  CostScaling(int N, int M=0) : N(N), capacity(2 * N) {
    if (M > 0) in.reserve(M);
  }

  void add_directed_edge(int u, int v, cap_t b, cap_t c, cost_t cost) {
    if (b > 0) capacity[v] += b, capacity[u + N] += b;
    else capacity[u] += -b, capacity[v + N] += -b;
    in.push_back({u, v, b, c, cost});
  }

  pair<bool, tcost_t> minimum_cost_circulation() {
    construct();
    if (!has_feasible_circulation()) return {false, 0};

    const int cost_multiplier = 2 << __lg(N); // should be > |V|
    cost_t eps = 0;
    for (auto& e : edges) e.cost *= cost_multiplier, eps = max(eps, e.cost);
    
    while (eps > 1) refine(eps = max<cost_t>(1, eps / alpha));

    tcost_t ret = initial_cost;
    for (auto& e : edges) ret -= (e.cost / cost_multiplier) * e.cap;
    return {true, ret / 2};
  }

private:
  void refine(const cost_t eps) {
    auto cost_p = [&] (int u, const Edge& e) {
      return e.cost + potential[u] - potential[e.to];
    };
    for (int u = 0; u < N; ++u) for (int i = ofs[u]; i < ofs[u + 1]; ++i) {
      auto& e = edges[i];
      if (cost_p(u, e) < 0) edges[e.rev].cap += e.cap, e.cap = 0;
    }
    vector<tcap_t> excess(initial_excess);
    for (auto& e : edges) excess[e.to] -= e.cap;

    vector<int> stack; stack.reserve(N);
    for (int u = 0; u < N; ++u) if (excess[u] > 0) stack.push_back(u);

    auto residue = [&] (const Edge& e) -> cap_t { return e.cap; };
    auto push = [&] (int u, Edge& e, cap_t df) {
      e.cap -= df; edges[e.rev].cap += df;
      excess[e.to] += df; excess[u] -= df;
      if (excess[e.to] > 0 && excess[e.to] <= df) {
        stack.push_back(e.to);
      }
    };
    auto relabel = [&] (int u, cost_t delta) {
      potential[u] -= delta + eps;
    };
    auto relabel_in_advance = [&] (int u) {
      if (excess[u] != 0) return false;
      auto delta = Inf;
      for (int ei = ofs[u]; ei < ofs[u + 1]; ++ei) {
        auto& e = edges[ei];
        if (residue(e) == 0) continue;
        if (cost_p(u, e) < 0) return false;
        else delta = min<tcost_t>(delta, cost_p(u, e));
      }
      relabel(u, delta);
      return true;
    };
    auto discharge = [&] (int u) {
      auto delta = Inf;
      for (int ei = ofs[u]; ei < ofs[u + 1]; ++ei) {
        auto& e = edges[ei];
        if (residue(e) == 0) continue;
        if (cost_p(u, e) < 0) {
          if (relabel_in_advance(e.to)) {
            --ei; continue; // modify ei (!)
          }
          cap_t df = min<tcap_t>(excess[u], residue(e));
          push(u, e, df);
          if (!excess[u]) return;
        } else delta = min<tcost_t>(delta, cost_p(u, e));
      }
      relabel(u, delta);
      stack.push_back(u);
    };
    while (!stack.empty()) {
      auto u = stack.back(); stack.pop_back();
      discharge(u);
    }
  }

  void construct() {
    ofs.assign(N + 1, 0);
    edges.resize(2 * in.size());
    initial_excess.assign(N, 0);
    initial_cost = 0;
    potential.assign(N, 0);
    for (auto& e : in) ofs[e.from + 1]++, ofs[e.to + 1]++;
    for (int i = 1; i <= N; ++i) ofs[i] += ofs[i - 1];
    for (auto& e : in) {
      initial_excess[e.to] += e.c;
      initial_excess[e.from] += -e.b;
      initial_cost += tcost_t(e.cost) * (e.c + e.b);
      edges[ofs[e.from]++] = {e.to, ofs[e.to], e.c - e.b, e.cost};
      edges[ofs[e.to]++] = {e.from, ofs[e.from] - 1, 0, -e.cost};
    }
    for (int i = N; i > 0; --i) ofs[i] = ofs[i - 1];
    ofs[0] = 0;
  }

  bool has_feasible_circulation() {
    return Dinic(N, ofs, edges, capacity).succeeded();
  }

private:
  int N; 
  vector<InputEdge> in;
  vector<tcap_t> capacity;

  vector<int> ofs;
  vector<Edge> edges;

  tcost_t initial_cost;
  vector<tcap_t> initial_excess;
  vector<tcost_t> potential;
};
// cap, total_cap, cost * (2 * |V|), total_cost
using MCC = CostScaling<int, int64_t, int64_t, int64_t>;
// using MCC = CostScaling<int, int, int, int>;

int main(){
  int n;
  cin >> n;

  vector<pair<int,int>> v(n);
  cin >> v;

  // Min_Cost_Flow<long long> f(n + n + 1 + 2, 1000000000);
  MCC f(n+n+3);
  int source = n+n+1;
  int sink = source+1;
  int odd = n+n;
  // f.add_edge(odd, sink, n/3, 0);
  f.add_directed_edge(odd, sink, n/3, n/3, 0);
  for(int i=0; i<n; i++){
    // f.add_edge(source, i, 1, v[i].first);
    f.add_directed_edge(source, i, 0, 1, v[i].first);
    for(int j=0; j<n-n/3; j++){
      // f.add_edge(i, n+j, 1, v[i].second * j);
      f.add_directed_edge(i, n+j, 0, 1, v[i].second * j);
    }
    // f.add_edge(i, odd, 1, -v[i].first);
    f.add_directed_edge(i, odd, 0, 1, -v[i].first);
  }
  for(int i=0; i<n-n/3; i++){
    // f.add_edge(n+i, sink, 1, 0);
    f.add_directed_edge(n+i, sink, 1, 1, 0);
  }

  f.add_directed_edge(sink, source, n, n, 0);

  // long long cost = f.min_cost_flow(source, sink, n);
  long long cost = f.minimum_cost_circulation().second;
  println(cost);

  return 0;
}
0