結果

問題 No.580 旅館の予約計画
ユーザー beetbeet
提出日時 2019-04-19 16:15:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 2,972 bytes
コンパイル時間 3,108 ms
コンパイル使用メモリ 231,296 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-23 17:36:54
合計ジャッジ時間 5,169 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 4 ms
4,348 KB
testcase_12 AC 6 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 33 ms
4,372 KB
testcase_16 AC 20 ms
4,372 KB
testcase_17 AC 20 ms
4,372 KB
testcase_18 AC 19 ms
4,372 KB
testcase_19 AC 26 ms
4,348 KB
testcase_20 AC 15 ms
4,372 KB
testcase_21 AC 14 ms
4,348 KB
testcase_22 AC 5 ms
4,348 KB
testcase_23 AC 9 ms
4,372 KB
testcase_24 AC 12 ms
4,372 KB
testcase_25 AC 9 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 9 ms
4,348 KB
testcase_31 AC 14 ms
4,372 KB
testcase_32 AC 11 ms
4,368 KB
testcase_33 AC 26 ms
4,372 KB
testcase_34 AC 7 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 T>
struct PrimalDual{
  struct edge{
    Int to;
    T cap,cost;
    Int rev;
    edge(){}
    edge(Int to,T cap,T cost,Int rev):to(to),cap(cap),cost(cost),rev(rev){}
  };
  
  T INF;
  vector<vector<edge> > G;
  vector<T> h,dist;
  vector<Int> prevv,preve;

  PrimalDual(){}
  PrimalDual(Int n,T INF):INF(INF),G(n),h(n),dist(n),prevv(n),preve(n){}
  
  void add_edge(Int from,Int to,T cap,T cost){
    G[from].emplace_back(to,cap,cost,G[to].size());
    G[to].emplace_back(from,0,-cost,G[from].size()-1);
  }

  T flow(Int s,Int t,T f,Int &ok){    
    T res=0;
    fill(h.begin(),h.end(),0);
    while(f>0){
      using P = pair<T, Int>;
      priority_queue<P,vector<P>,greater<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[e.to]>dist[v]+e.cost+h[v]-h[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);
          }
        }
      }
      if(dist[t]==INF) return ok=0;
      
      for(Int v=0;v<(Int)h.size();v++) h[v]+=dist[v];

      T d=f;
      for(Int v=t;v!=s;v=prevv[v])
        d=min(d,G[prevv[v]][preve[v]].cap);
      
      f-=d;
      res+=d*h[t];
      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 T>
vector<T> compress(vector<T> v){
  sort(v.begin(),v.end());
  v.erase(unique(v.begin(),v.end()),v.end());
  return v;
}

template<typename T>
map<T, Int> dict(const vector<T> &v){
  map<T, Int> res;
  for(Int i=0;i<(Int)v.size();i++)
    res[v[i]]=i;
  return res;
}

//INSERT ABOVE HERE
signed main(){
  Int n,m;
  scanf("%lld %lld",&n,&m);
  vector<Int> ls(m),rs(m);
  for(Int i=0;i<m;i++){
    {
      Int d,h,m;
      scanf("%lld %02lld:%02lld",&d,&h,&m);
      ls[i]=d*24*60+h*60+m;
    }    
    {
      Int d,h,m;
      scanf("%lld %02lld:%02lld",&d,&h,&m);
      rs[i]=d*24*60+h*60+m;
    }  
  }
  vector<Int> vs;
  for(Int x:ls) vs.emplace_back(x);
  for(Int x:rs) vs.emplace_back(x+1);
  vs.emplace_back(0);
  vs.emplace_back(1e8);

  vs=compress(vs);
  auto dc=dict(vs);
  
  Int d=vs.size();
  const Int INF = 1e8;
  PrimalDual<Int> G(d,INF);
  for(Int i=0;i+1<d;i++)
    G.add_edge(i,i+1,INF,0);     

  for(Int i=0;i<m;i++)
    G.add_edge(dc[ls[i]],dc[rs[i]+1],1,-1);

  Int ok=0;
  printf("%lld\n",-G.flow(0,d-1,n,ok));
  assert(ok);
  return 0;
}
0