提出 #7977363


ソースコード 拡げる

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
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;}

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;
  vector<vector<edge>> G;
  vector<TC> h,dist;
  vector<int> prevv,preve;

  PrimalDual(){}
  PrimalDual(int n):G(n),h(n),dist(n),prevv(n),preve(n){}

  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){
    struct P{
      TC first;
      int second;
      P(TC first,int second):first(first),second(second){}
      bool operator<(const P&a) const{return a.first<first;}
    };
    priority_queue<P> que;
    fill(dist.begin(),dist.end(),INF);

    dist[s]=0;
    que.emplace(dist[s],s);
    while(!que.empty()){
      P p=que.top();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.emplace(dist[e.to],e.to);
        }
      }
    }
  }

  TC flow(int s,int t,TF f,int &ok){
    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);

      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;
#endif
//BEGIN CUT HERE
template<typename TF,typename TC>
struct NegativeEdge{
  PrimalDual<TF, TC> G;
  vector<TF> fs;
  TC sum;
  int S,T;
  NegativeEdge(){}
  NegativeEdge(int n):G(n+2),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 &ok){
    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);
  }

  TC flow(int ts,int tt,TF tf,int &ok){
    fs[ts]+=tf;
    fs[tt]-=tf;
    return flow(ok);
  }
};
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed CFR190_B(){
  using ll = long long;
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,m;
  cin>>n>>m;
  vector<string> vp(n);
  vector<int> vs(n);
  for(int i=0;i<n;i++) cin>>vp[i]>>vs[i];
  vector<int> ss(m);
  for(int i=0;i<m;i++) cin>>ss[i];

  int S=n+m,T=n+m+1;
  NegativeEdge<ll, ll> G(n+m+2);

  for(int i=0;i<m;i++){
    G.add_edge(S,i,1,0);
    for(int j=0;j<n;j++){
      if(vp[j]=="ATK"){
        if(ss[i]>=vs[j]) G.add_edge(i,m+j,1,vs[j]-ss[i]);
      }
      if(vp[j]=="DEF"){
        if(ss[i]> vs[j]) G.add_edge(i,m+j,1,0);
      }
    }
  }

  auto H=G;
  for(int i=0;i<m;i++){
    G.add_edge(i,T,1,-ss[i]);
    H.add_edge(i,T,1,0);
  }

  for(int j=0;j<n;j++){
    G.use_edge(m+j,T,1,0);
    H.add_edge(m+j,T,1,0);
  }

  int ok;
  ll ans=0;
  if(n<m){
    ll gv=G.flow(S,T,m,ok);
    if(ok) chmin(ans,gv);
  }
  ll hv=H.flow(S,T,m,ok);
  if(ok) chmin(ans,hv);

  cout<<-ans<<endl;
  return 0;
}
/*
  verified on 2019/10/13
  https://codeforces.com/contest/321/problem/B
*/

signed KUPC2019_H(){
  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,2);
    G.add_edge(i,n,cap,0);
  }

  int ok=0;
  cout<<-G.flow(n,n,0,ok)<<endl;
  return 0;
}
/*
  verified on 2019/10/15
  https://atcoder.jp/contests/kupc2019/tasks/kupc2019_h
*/

signed main(){
  //CFR190_B();
  KUPC2019_H();
  return 0;
}
#endif

提出情報

提出日時
問題 H - 123パズル
ユーザ beet
言語 C++14 (GCC 5.4.1)
得点 300
コード長 4832 Byte
結果 AC
実行時間 1370 ms
メモリ 1404 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 1 ms 256 KiB
handmade_01 AC 1 ms 256 KiB
marathon_killer_00 AC 34 ms 512 KiB
marathon_killer_01 AC 42 ms 512 KiB
marathon_killer_02 AC 24 ms 512 KiB
marathon_killer_03 AC 31 ms 512 KiB
marathon_killer_04 AC 32 ms 512 KiB
marathon_killer_05 AC 25 ms 512 KiB
marathon_killer_06 AC 25 ms 512 KiB
marathon_killer_07 AC 30 ms 512 KiB
marathon_killer_08 AC 26 ms 512 KiB
marathon_killer_09 AC 29 ms 512 KiB
marathon_killer_10 AC 25 ms 512 KiB
marathon_killer_11 AC 22 ms 512 KiB
marathon_killer_12 AC 28 ms 512 KiB
marathon_killer_13 AC 25 ms 512 KiB
marathon_killer_14 AC 62 ms 640 KiB
marathon_killer_15 AC 100 ms 640 KiB
marathon_killer_16 AC 145 ms 640 KiB
marathon_killer_17 AC 48 ms 640 KiB
marathon_killer_18 AC 81 ms 640 KiB
marathon_killer_19 AC 120 ms 640 KiB
marathon_killer_20 AC 41 ms 640 KiB
marathon_killer_21 AC 49 ms 640 KiB
marathon_killer_22 AC 90 ms 640 KiB
near_dag_00 AC 637 ms 896 KiB
near_dag_01 AC 696 ms 896 KiB
near_dag_02 AC 1281 ms 1276 KiB
near_dag_03 AC 928 ms 1152 KiB
near_dag_04 AC 1165 ms 1276 KiB
rand_dense_00 AC 16 ms 512 KiB
rand_dense_01 AC 20 ms 512 KiB
rand_dense_02 AC 8 ms 384 KiB
rand_dense_03 AC 17 ms 512 KiB
rand_dense_04 AC 4 ms 384 KiB
rand_sparse_00 AC 925 ms 1152 KiB
rand_sparse_01 AC 1370 ms 1404 KiB
rand_sparse_02 AC 602 ms 896 KiB
rand_sparse_03 AC 1008 ms 1152 KiB
rand_sparse_04 AC 511 ms 896 KiB
sample_00 AC 1 ms 256 KiB
sample_01 AC 1 ms 256 KiB
small_00 AC 1 ms 256 KiB
small_01 AC 1 ms 256 KiB
small_02 AC 1 ms 256 KiB
star_00 AC 865 ms 1276 KiB
star_01 AC 860 ms 1276 KiB
star_02 AC 877 ms 1280 KiB
star_03 AC 975 ms 1276 KiB
star_04 AC 967 ms 1280 KiB