提出 #7960310


ソースコード 拡げる

#include<bits/stdc++.h>
using namespace std;
using Int = signed;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;


const int MAX = 5050;
array< pair< int, int >, MAX> v[33];
struct RadixHeap
{
  int size, last;
  array<int, 33> pos;

  RadixHeap() : size(0), last(0) {
    fill(pos.begin(),pos.end(),0);
  }

  bool empty() const { return size == 0; }

  inline int getbit(int a)
  {
    return a ? 32 - __builtin_clz(a) : 0;
  }

  void push(int key, const int &value)
  {
    ++size;
    int bit=getbit(key ^ last);
    v[bit][pos[bit]++]=make_pair(key, value);
  }

  pair< int, int > pop()
  {
    if(pos[0]==0) {
      int idx = 1;
      while(pos[idx]==0) ++idx;
      last = min_element(begin(v[idx]), begin(v[idx])+pos[idx])->first;
      for(auto &p : v[idx]){
        int bit=getbit(p.first ^ last);
        v[bit][pos[bit]++]=p;
      }
      pos[idx]=0;
    }
    --size;
    pos[0]--;
    auto ret = v[0][pos[0]];
    return ret;
  }
};

template<typename TF,typename TC>
struct PrimalDual{
  struct edge{
    int to;
    TF cap;
    TC cost;
    int rev;
    edge(){}
    edge(int to,TF cap,TC cost,int rev):
      to(to),cap(cap),cost(cost),rev(rev){}
  };

  static const TC INF;
  array<vector<edge>, MAX> G;
  array<TC, MAX> h,dist;
  array<int, MAX> prevv,preve;

  void add_edge(int u,int v,TF cap,TC cost){
    G[u].emplace_back(v,cap,cost,G[v].size());
    G[v].emplace_back(u,0,-cost,G[u].size()-1);
  }

  void dijkstra(int s){
    RadixHeap que;
    fill(dist.begin(),dist.end(),INF);

    dist[s]=0;
    que.push(dist[s],s);
    while(!que.empty()){
      auto p=que.pop();
      int v=p.second;
      if(dist[v]<p.first) continue;
      for(int i=0;i<(int)G[v].size();i++){
        edge &e=G[v][i];
        if(e.cap==0) continue;
        if(dist[v]+e.cost+h[v]-h[e.to]<dist[e.to]){
          dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
          prevv[e.to]=v;
          preve[e.to]=i;
          que.push(dist[e.to],e.to);
        }
      }
    }
  }

  TC flow(int s,int t,TF f,int &ok){
    TF flow=0;
    TC res=0;
    fill(h.begin(),h.end(),0);
    while(f>0){
      dijkstra(s);
      if(dist[t]==INF){
        ok=0;
        return res;
      }

      for(int v=0;v<(int)h.size();v++)
        if(dist[v]<INF) h[v]=h[v]+dist[v];

      TF d=f;
      for(int v=t;v!=s;v=prevv[v])
        d=min(d,G[prevv[v]][preve[v]].cap);

      flow+=d;
      f-=d;
      res=res+h[t]*d;
      for(int v=t;v!=s;v=prevv[v]){
        edge &e=G[prevv[v]][preve[v]];
        e.cap-=d;
        G[v][e.rev].cap+=d;
      }
    }
    ok=1;
    return res;
  }
};
template<typename TF, typename TC>
const TC PrimalDual<TF, TC>::INF = numeric_limits<TC>::max()/2;

template<typename TF,typename TC>
struct NegativeEdge{
  PrimalDual<TF, TC> G;
  vector<TF> fs;
  TC sum;
  int S,T;
  NegativeEdge(){}
  NegativeEdge(int n):fs(n+2,0),sum(0),S(n),T(n+1){}

  void use_edge(int u,int v,TF cap,TC cost){
    fs[u]-=cap;
    fs[v]+=cap;
    sum=sum+cost*cap;
  }

  void add_edge(int u,int v,TF cap,TC cost){
    if(cost<TC(0)){
      use_edge(u,v,cap,cost);
      swap(u,v);
      cost=-cost;
    }
    G.add_edge(u,v,cap,cost);
  }

  TC flow(int ts,int tt,TF tf,int &ok){
    fs[ts]+=tf;
    fs[tt]-=tf;

    TF f=0;
    for(int i=0;i<S;i++){
      if(fs[i]>0){
        f+=fs[i];
        G.add_edge(S,i,+fs[i],TC(0));
      }
      if(fs[i]<0){
        G.add_edge(i,T,-fs[i],TC(0));
      }
    }
    return sum+G.flow(S,T,f,ok);
  }
};

//INSERT ABOVE HERE
signed main(){
  int n,m;
  cin>>n>>m;
  NegativeEdge<int, int> G(n+2);

  int cap=n+1;
  for(int i=0;i<m;i++){
    int u,v,l;
    cin>>u>>v>>l;
    u--;v--;
    G.add_edge(v,u,1,l-1);
    if(l==0) cap++;
  }

  for(int i=0;i<n;i++){
    G.add_edge(n,i,cap,+3);
    G.add_edge(i,n,cap,-1);
  }
  int ok=0;
  cout<<-G.flow(n,n,0,ok)<<endl;
  return 0;
}

提出情報

提出日時
問題 H - 123パズル
ユーザ beet
言語 C++14 (GCC 5.4.1)
得点 300
コード長 4213 Byte
結果 AC
実行時間 1779 ms
メモリ 1660 KiB

ジャッジ結果

セット名 All Sample
得点 / 配点 300 / 300 0 / 0
結果
AC × 50
AC × 2
セット名 テストケース
All handmade_00, handmade_01, marathon_killer_00, marathon_killer_01, marathon_killer_02, marathon_killer_03, marathon_killer_04, marathon_killer_05, marathon_killer_06, marathon_killer_07, marathon_killer_08, marathon_killer_09, marathon_killer_10, marathon_killer_11, marathon_killer_12, marathon_killer_13, marathon_killer_14, marathon_killer_15, marathon_killer_16, marathon_killer_17, marathon_killer_18, marathon_killer_19, marathon_killer_20, marathon_killer_21, marathon_killer_22, near_dag_00, near_dag_01, near_dag_02, near_dag_03, near_dag_04, rand_dense_00, rand_dense_01, rand_dense_02, rand_dense_03, rand_dense_04, rand_sparse_00, rand_sparse_01, rand_sparse_02, rand_sparse_03, rand_sparse_04, sample_00, sample_01, small_00, small_01, small_02, star_00, star_01, star_02, star_03, star_04
Sample sample_00, sample_01
ケース名 結果 実行時間 メモリ
handmade_00 AC 2 ms 512 KiB
handmade_01 AC 1 ms 512 KiB
marathon_killer_00 AC 47 ms 768 KiB
marathon_killer_01 AC 55 ms 768 KiB
marathon_killer_02 AC 34 ms 768 KiB
marathon_killer_03 AC 43 ms 768 KiB
marathon_killer_04 AC 45 ms 768 KiB
marathon_killer_05 AC 36 ms 768 KiB
marathon_killer_06 AC 38 ms 768 KiB
marathon_killer_07 AC 40 ms 768 KiB
marathon_killer_08 AC 35 ms 768 KiB
marathon_killer_09 AC 40 ms 768 KiB
marathon_killer_10 AC 34 ms 768 KiB
marathon_killer_11 AC 31 ms 768 KiB
marathon_killer_12 AC 39 ms 768 KiB
marathon_killer_13 AC 36 ms 896 KiB
marathon_killer_14 AC 76 ms 896 KiB
marathon_killer_15 AC 116 ms 768 KiB
marathon_killer_16 AC 158 ms 896 KiB
marathon_killer_17 AC 64 ms 896 KiB
marathon_killer_18 AC 103 ms 896 KiB
marathon_killer_19 AC 145 ms 896 KiB
marathon_killer_20 AC 59 ms 896 KiB
marathon_killer_21 AC 82 ms 896 KiB
marathon_killer_22 AC 127 ms 896 KiB
near_dag_00 AC 657 ms 1152 KiB
near_dag_01 AC 713 ms 1152 KiB
near_dag_02 AC 1442 ms 1404 KiB
near_dag_03 AC 1076 ms 1280 KiB
near_dag_04 AC 1454 ms 1408 KiB
rand_dense_00 AC 23 ms 768 KiB
rand_dense_01 AC 28 ms 768 KiB
rand_dense_02 AC 14 ms 640 KiB
rand_dense_03 AC 24 ms 768 KiB
rand_dense_04 AC 8 ms 640 KiB
rand_sparse_00 AC 1113 ms 1280 KiB
rand_sparse_01 AC 1779 ms 1532 KiB
rand_sparse_02 AC 706 ms 1152 KiB
rand_sparse_03 AC 1260 ms 1280 KiB
rand_sparse_04 AC 555 ms 1152 KiB
sample_00 AC 2 ms 512 KiB
sample_01 AC 2 ms 512 KiB
small_00 AC 2 ms 512 KiB
small_01 AC 2 ms 512 KiB
small_02 AC 3 ms 512 KiB
star_00 AC 1377 ms 1660 KiB
star_01 AC 1379 ms 1660 KiB
star_02 AC 1384 ms 1660 KiB
star_03 AC 1378 ms 1660 KiB
star_04 AC 1402 ms 1532 KiB