結果

問題 No.1615 Double Down
ユーザー LayCurseLayCurse
提出日時 2021-07-21 21:51:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 11,693 bytes
コンパイル時間 2,627 ms
コンパイル使用メモリ 219,836 KB
実行使用メモリ 18,128 KB
最終ジャッジ日時 2023-09-24 16:54:37
合計ジャッジ時間 26,200 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline")
#include<bits/stdc++.h>
using namespace std;
template<class T> struct cLtraits_identity{
  using type = T;
}
;
template<class T> using cLtraits_try_make_signed =
  typename conditional<
    is_integral<T>::value,
    make_signed<T>,
    cLtraits_identity<T>
    >::type;
template <class S, class T> struct cLtraits_common_type{
  using tS = typename cLtraits_try_make_signed<S>::type;
  using tT = typename cLtraits_try_make_signed<T>::type;
  using type = typename common_type<tS,tT>::type;
}
;
void*wmem;
char memarr[96000000];
template<class S, class T> inline auto min_L(S a, T b)
-> typename cLtraits_common_type<S,T>::type{
  return (typename cLtraits_common_type<S,T>::type) a <= (typename cLtraits_common_type<S,T>::type) b ? a : b;
}
template<class S, class T> inline auto max_L(S a, T b)
-> typename cLtraits_common_type<S,T>::type{
  return (typename cLtraits_common_type<S,T>::type) a >= (typename cLtraits_common_type<S,T>::type) b ? a : b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
  walloc1d(arr, x2-x1, mem);
  (*arr) -= x1;
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(unsigned x){
  int s=0;
  char f[10];
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(unsigned long long x){
  int s=0;
  char f[21];
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
int WRITER_DOUBLE_DIGIT = 15;
inline int writerDigit_double(){
  return WRITER_DOUBLE_DIGIT;
}
inline void writerDigit_double(int d){
  WRITER_DOUBLE_DIGIT = d;
}
inline void wt_L(double x){
  const int d = WRITER_DOUBLE_DIGIT;
  int k;
  int r;
  double v;
  if(x!=x || (x==x+1 && x==2*x)){
    my_putchar_unlocked('E');
    my_putchar_unlocked('r');
    my_putchar_unlocked('r');
    return;
  }
  if(x < 0){
    my_putchar_unlocked('-');
    x = -x;
  }
  x += 0.5 * pow(0.1, d);
  r = 0;
  v = 1;
  while(x >= 10*v){
    v *= 10;
    r++;
  }
  while(r >= 0){
    r--;
    k = floor(x / v);
    if(k >= 10){
      k = 9;
    }
    if(k <= -1){
      k = 0;
    }
    x -= k * v;
    v *= 0.1;
    my_putchar_unlocked(k + '0');
  }
  if(d > 0){
    my_putchar_unlocked('.');
    v = 1;
    for(r=(0);r<(d);r++){
      v *= 0.1;
      k = floor(x / v);
      if(k >= 10){
        k = 9;
      }
      if(k <= -1){
        k = 0;
      }
      x -= k * v;
      my_putchar_unlocked(k + '0');
    }
  }
}
inline void wt_L(const char c[]){
  int i=0;
  for(i=0;c[i]!='\0';i++){
    my_putchar_unlocked(c[i]);
  }
}
inline void wt_L(string &x){
  int i=0;
  for(i=0;x[i]!='\0';i++){
    my_putchar_unlocked(x[i]);
  }
}
template<class S, class T> inline S chmin(S &a, T b){
  if(a>b){
    a=b;
  }
  return a;
}
template <class T> struct LHeap{
  int*hp;
  int*place;
  int size;
  T*val;
  void malloc(int N){
    hp = (int*)std::malloc(N*sizeof(int));
    place=(int*)std::malloc(N*sizeof(int));
    val=(T*)std::malloc(N*sizeof(T));
  }
  void malloc(int N, int ini){
    hp = (int*)std::malloc(N*sizeof(int));
    place=(int*)std::malloc(N*sizeof(int));
    val=(T*)std::malloc(N*sizeof(T));
    if(ini){
      init(N);
    }
  }
  void walloc(int N, void **mem=&wmem){
    walloc1d(&hp, N, mem);
    walloc1d(&place, N, mem);
    walloc1d(&val, N, mem);
  }
  void walloc(int N, int ini, void **mem=&wmem){
    walloc1d(&hp, N, mem);
    walloc1d(&place, N, mem);
    walloc1d(&val, N, mem);
    if(ini){
      init(N);
    }
  }
  void free(){
    std::free(hp);
    std::free(place);
    std::free(val);
  }
  void init(int N){
    int i;
    size=0;
    for(i=(0);i<(N);i++){
      place[i]=-1;
    }
  }
  void up(int n){
    int m;
    while(n){
      m=(n-1)/2;
      if(val[hp[m]]<=val[hp[n]]){
        break;
      }
      swap(hp[m],hp[n]);
      swap(place[hp[m]],place[hp[n]]);
      n=m;
    }
  }
  void down(int n){
    int m;
    for(;;){
      m=2*n+1;
      if(m>=size){
        break;
      }
      if(m+1<size&&val[hp[m]]>val[hp[m+1]]){
        m++;
      }
      if(val[hp[m]]>=val[hp[n]]){
        break;
      }
      swap(hp[m],hp[n]);
      swap(place[hp[m]],place[hp[n]]);
      n=m;
    }
  }
  void change(int n, T v){
    T f = val[n];
    val[n] = v;
    if(place[n]==-1){
      place[n] = size;
      hp[size++] = n;
      up(place[n]);
    }
    else{
      if(f < v){
        down(place[n]);
      }
      else if(f > v){
        up(place[n]);
      }
    }
  }
  int pop(void){
    int res = hp[0];
    place[res] = -1;
    size--;
    if(size){
      hp[0]=hp[size];
      place[hp[0]]=0;
      down(0);
    }
    return res;
  }
}
;
template<class FT, class CT> struct minCostFlow{
  int node;
  int*es;
  int*emem;
  int**edge;
  int**rev;
  FT**flow;
  FT f_eps;
  CT**cost;
  CT*potential;
  CT c_eps;
  LHeap<CT> hp;
  char*reached;
  FT*cur_flow;
  CT*cur_cost;
  int*back_edge;
  void malloc(int N){
    int i;
    es = (int*)std::malloc(N*sizeof(int));
    emem = (int*)std::malloc(N*sizeof(int));
    edge = (int**)std::malloc(N*sizeof(int*));
    rev = (int**)std::malloc(N*sizeof(int*));
    flow = (FT**)std::malloc(N*sizeof(FT*));
    cost = (CT**)std::malloc(N*sizeof(CT*));
    for(i=(0);i<(N);i++){
      emem[i] = 0;
      edge[i] = rev[i] = NULL;
      flow[i] = NULL;
      cost[i] = NULL;
    }
    hp.malloc(N);
    reached = (char*)std::malloc(N*sizeof(char));
    cur_flow = (FT*)std::malloc(N*sizeof(FT));
    cur_cost = (CT*)std::malloc(N*sizeof(CT));
    potential = (CT*)std::malloc(N*sizeof(CT));
    back_edge = (int*)std::malloc(N*sizeof(int));
    node = N;
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    f_eps = (FT)1e-9;
    c_eps = (CT)1e-9;
  }
  void init(int N){
    int i;
    node = N;
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    f_eps = (FT)1e-9;
    c_eps = (CT)1e-9;
  }
  void memoryExpand(int i, int sz){
    if(sz <= emem[i]){
      return;
    }
    sz =max_L(max_L(sz, 3), 2*emem[i]);
    emem[i] = sz;
    edge[i] = (int*)realloc(edge[i], sz*sizeof(int));
    rev[i] = (int*)realloc(rev[i], sz*sizeof(int));
    flow[i] = (FT*)realloc(flow[i], sz*sizeof(FT));
    cost[i] = (CT*)realloc(cost[i], sz*sizeof(CT));
  }
  void addEdge(int n1, int n2, FT f, CT c){
    int s1 = es[n1]++;
    int s2 = es[n2]++;
    if(s1 >= emem[n1]){
      memoryExpand(n1, es[n1]);
    }
    if(s2 >= emem[n2]){
      memoryExpand(n2, es[n2]);
    }
    edge[n1][s1] = n2;
    edge[n2][s2] = n1;
    rev[n1][s1]  = s2;
    rev[n2][s2]  = s1;
    flow[n1][s1] = f;
    flow[n2][s2] = 0;
    cost[n1][s1] = c;
    cost[n2][s2] = -c;
  }
  template<class FTS, class CTS> void solve(int st, int ed, FTS &fres, CTS &cres, FT flim = -1, CT clim = 0){
    int i;
    int j;
    int k;
    int l;
    FT f;
    CT nc;
    fres = 0;
    cres = 0;
    for(i=(0);i<(node);i++){
      potential[i] = 0;
    }
    for(;;){
      if(flim >= -f_eps && flim <= f_eps){
        break;
      }
      hp.init(node);
      for(i=(0);i<(node);i++){
        reached[i] = 0;
      }
      reached[st] = 1;
      cur_cost[st] = 0;
      l = 0;
      hp.change(st, cur_cost[st]);
      while(hp.size){
        i = hp.pop();
        for(j=(0);j<(es[i]);j++){
          if(flow[i][j] <= f_eps){
            continue;
          }
          k = edge[i][j];
          nc = cur_cost[i] + cost[i][j] + potential[i] - potential[k];
          if(reached[k]==0 || cur_cost[k] > nc+c_eps){
            reached[k] = 1;
            cur_cost[k] = nc;
            cur_flow[k] = flow[i][j];
            if(i != st){
              chmin(cur_flow[k], cur_flow[i]);
            }
            back_edge[k] = rev[i][j];
            hp.change(k, cur_cost[k]);
          }
        }
      }
      if(reached[ed]==0){
        break;
      }
      if(flim==-2 && cur_cost[ed] + potential[ed] >= clim){
        break;
      }
      f = cur_flow[ed];
      if(flim >= -f_eps){
        chmin(f, flim);
        flim -= f;
      }
      if(f <= f_eps){
        break;
      }
      for(i=(0);i<(node);i++){
        if(reached[i]){
          potential[i] += cur_cost[i];
        }
      }
      fres += f;
      cres += f * potential[ed];
      i = ed;
      while(i != st){
        j = back_edge[i];
        k = edge[i][j];
        flow[i][j] += f;
        flow[k][rev[i][j]] -= f;
        i = k;
      }
    }
  }
}
;
int N;
int M;
int K;
int L;
int X[100000];
int Y[100000];
int Z[100000];
int main(){
  int i;
  wmem = memarr;
  int node;
  int st;
  int ed;
  minCostFlow<int,long long> f;
  int flow;
  long long cost;
  long long res = 0;
  rd(N);
  rd(M);
  rd(K);
  rd(L);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(L);Lj4PdHRW++){
      rd(X[Lj4PdHRW]);X[Lj4PdHRW] += (-1);
      rd(Y[Lj4PdHRW]);Y[Lj4PdHRW] += (-1);
      rd(Z[Lj4PdHRW]);
    }
  }
  node = N + M;
  st = node++;
  ed = node++;
  f.malloc(node);
  f.init(node);
  for(i=(0);i<(N);i++){
    f.addEdge(st, i, 1, 0);
  }
  for(i=(0);i<(M);i++){
    f.addEdge(i+N, ed, 1, 0);
  }
  for(i=(0);i<(L);i++){
    f.addEdge(X[i], Y[i]+N, 1, 1073709056-(1<<Z[i]));
  }
  f.solve(st, ed, flow, cost, -2, 1073709056);
  wt_L((long long)flow * 1073709056 - cost);
  wt_L('\n');
  return 0;
}
// cLay version 20210717-1 [beta]

// --- original code ---
// int N, M, K, L, X[1d5], Y[], Z[];
// 
// {
//   int node, st, ed;
//   minCostFlow<int,ll> f;
//   int flow; ll cost, res = 0;
//   rd(N,M,K,L,(X--,Y--,Z)(L));
// 
//   node = N + M;
//   st = node++;
//   ed = node++;
//   f.malloc(node);
//   f.init(node);
//   rep(i,N) f.addEdge(st, i, 1, 0);
//   rep(i,M) f.addEdge(i+N, ed, 1, 0);
//   rep(i,L) f.addEdge(X[i], Y[i]+N, 1, int_inf-(1<<Z[i]));
//   f.solve(st, ed, flow, cost, -2, int_inf);
//   wt((ll)flow * int_inf - cost);
// }
0