結果

問題 No.654 Air E869120
ユーザー HaarHaar
提出日時 2019-05-10 00:56:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 4,399 bytes
コンパイル時間 2,353 ms
コンパイル使用メモリ 196,232 KB
実行使用メモリ 35,072 KB
最終ジャッジ日時 2024-07-02 00:54:11
合計ジャッジ時間 5,729 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 28 ms
6,528 KB
testcase_11 AC 17 ms
6,784 KB
testcase_12 AC 14 ms
6,528 KB
testcase_13 AC 25 ms
6,656 KB
testcase_14 AC 15 ms
6,656 KB
testcase_15 AC 17 ms
6,656 KB
testcase_16 AC 205 ms
34,944 KB
testcase_17 AC 196 ms
34,944 KB
testcase_18 AC 188 ms
34,944 KB
testcase_19 AC 209 ms
34,944 KB
testcase_20 AC 134 ms
34,944 KB
testcase_21 AC 150 ms
34,944 KB
testcase_22 AC 63 ms
34,944 KB
testcase_23 AC 102 ms
34,944 KB
testcase_24 AC 207 ms
34,944 KB
testcase_25 AC 54 ms
34,944 KB
testcase_26 AC 95 ms
34,944 KB
testcase_27 AC 44 ms
34,944 KB
testcase_28 AC 75 ms
34,944 KB
testcase_29 AC 37 ms
34,816 KB
testcase_30 AC 26 ms
34,944 KB
testcase_31 AC 28 ms
34,944 KB
testcase_32 AC 28 ms
35,072 KB
testcase_33 AC 27 ms
34,944 KB
testcase_34 AC 28 ms
34,944 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define FOR(v, a, b) for(int v = (a); v < (b); ++v)
#define FORE(v, a, b) for(int v = (a); v <= (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define REV(v, a, b) for(int v = (a); v >= (b); --v)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it)
#define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it)
#define EXIST(c,x) ((c).find(x) != (c).end())
#define LLI long long int
#define fst first
#define snd second

#ifdef DEBUG
#include <misc/C++/Debug.cpp>
#else
#define dump(x) ((void)0)
#endif

#define gcd __gcd

using namespace std;
template <class T> constexpr T lcm(T m, T n){return m/gcd(m,n)*n;}

template <typename I> void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost<<d; ost<<*i;}ost<<endl;}
template <typename T> istream& operator>>(istream &is, vector<T> &v){for(auto &a : v) is >> a; return is;}
template <typename T, typename U> istream& operator>>(istream &is, pair<T,U> &p){is >> p.first >> p.second; return is;}

template <typename T, typename U> bool chmin(T &a, const U &b){return (a>b ? a=b, true : false);}
template <typename T, typename U> bool chmax(T &a, const U &b){return (a<b ? a=b, true : false);}
template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v){fill((U*)a, (U*)(a+N), v);}

template <typename T, T INF> class Dinic{
private:
  vector<vector<pair<int,T>>> graph;
  int size, s, t;
  vector<vector<T>> cap;
  vector<int> level;
  
  bool buildLevel(){
    fill(ALL(level), 0);
    level[s] = 1;
    deque<int> deq = {s};
    while(!deq.empty()){
      int cur = deq.front(); deq.pop_front();
      REP(i,size)
	if(level[i]==0 && cap[cur][i]>0){
	  level[i] = level[cur] + 1;
	  deq.push_back(i);
	}
    }
    return level[t] != 0;
  }
  void dfs(vector<int> &path, T &flow){
    if(path.empty()) return;
    int cur = path.back();
    if(cur == t){
      T f = INF;
      FOR(i,1,(int)path.size()) f = min(f, cap[path[i-1]][path[i]]);
      FOR(i,1,(int)path.size()){
	cap[path[i-1]][path[i]] -= f;
	cap[path[i]][path[i-1]] += f;
      }
      flow += f;
    }else{
      REP(i,size){
	if(cap[cur][i]>0 && level[i]>level[cur]){
	  path.push_back(i);
	  dfs(path, flow);
	  path.pop_back();
	}
      }
    }
  }
  T augment(){
    T f = 0;
    vector<int> path = {s};
    dfs(path, f);
    return f;
  }
  T loop(){
    T f = 0;
    while(buildLevel()) f += augment();
    return f;
  }
 
public:
  Dinic(vector<vector<pair<int,T>>> &_graph): graph(_graph), size(graph.size()) {}
  Dinic(int size): graph(size), size(size){}
 
  void add_edge(int from, int to, const T &cap){
    graph[from].push_back({to, cap});
  }
  
  T max_flow(int _s, int _t){
    cap = vector<vector<T>>(size, vector<T>(size, 0));
    level = vector<int>(size, 0);

    REP(i,size)
      for(auto &p : graph[i]){
	int j = p.first;
	T d = p.second;
	cap[i][j] += d;
      }
 
    s = _s;
    t = _t;
 
    return loop();
  }
};

const LLI inf = 1LL<<50;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N,M,d;
  while(cin >> N >> M >> d){
    vector<int> u(M), v(M), p(M), q(M), w(M);

    vector<map<int,int>> ps(N), qs(N);
    
    REP(i,M){
      cin >> u[i] >> v[i] >> p[i] >> q[i] >> w[i];
      --u[i];
      --v[i];

      ps[u[i]][p[i]] = 0;
      qs[v[i]][q[i]] = 0;
    }

    int s = 0;
    int t = 1;

    int k = 2;

    REP(i,N){
      ITR(it,ps[i]){
	it->snd = k;
	++k;
      }

      ITR(it,qs[i]){
	it->snd = k;
	++k;
      }
    }

    Dinic<LLI,inf> dinic(k);

    REP(i,M){
      dinic.add_edge(ps[u[i]][p[i]], qs[v[i]][q[i]], w[i]);
    }

    REP(i,N){
      {
	vector<int> temp;
	for(auto &kv : ps[i]) temp.push_back(kv.snd);
	REP(i,(int)temp.size()-1) dinic.add_edge(temp[i], temp[i+1], inf);
      }
      {
	vector<int> temp;
	for(auto &kv : qs[i]) temp.push_back(kv.snd);
	REP(i,(int)temp.size()-1) dinic.add_edge(temp[i], temp[i+1], inf);
      }

      for(auto &kv : qs[i]){
	auto it = ps[i].lower_bound(kv.fst+d);
	if(it == ps[i].end()) break;
	dinic.add_edge(kv.snd, it->snd, inf);
      }
    }
    
    ITR(it, ps[0]){
      dinic.add_edge(s, it->snd, inf);
    }

    ITR(it, qs[N-1]){
      dinic.add_edge(it->snd, t, inf);
    }

    LLI ans = dinic.max_flow(s,t);

    cout << ans << endl;
  }
  
  return 0;
}
0