結果

問題 No.654 Air E869120
ユーザー どららどらら
提出日時 2018-02-23 23:52:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,298 bytes
コンパイル時間 2,375 ms
コンパイル使用メモリ 194,056 KB
実行使用メモリ 24,064 KB
最終ジャッジ日時 2024-04-18 11:43:40
合計ジャッジ時間 3,596 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 7 ms
5,632 KB
testcase_16 AC 43 ms
23,936 KB
testcase_17 AC 42 ms
24,064 KB
testcase_18 AC 43 ms
23,936 KB
testcase_19 AC 44 ms
23,936 KB
testcase_20 AC 24 ms
13,696 KB
testcase_21 AC 24 ms
13,824 KB
testcase_22 AC 22 ms
13,824 KB
testcase_23 AC 21 ms
13,568 KB
testcase_24 AC 23 ms
13,696 KB
testcase_25 AC 18 ms
12,160 KB
testcase_26 AC 21 ms
13,184 KB
testcase_27 AC 14 ms
8,960 KB
testcase_28 AC 15 ms
9,068 KB
testcase_29 AC 12 ms
9,088 KB
testcase_30 AC 5 ms
5,376 KB
testcase_31 AC 5 ms
5,376 KB
testcase_32 AC 4 ms
5,376 KB
testcase_33 AC 5 ms
5,376 KB
testcase_34 AC 5 ms
5,376 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>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

const ll INF = (ll)(1e17);

class Dinic {
  int MAX_V;
  struct edge{ ll to, cap, rev, icap, flow; };

  vector< vector<edge> > G;
  vector<int> level; //sからの距離
  vector<int> iter; //どこまで調べたか

  void max_flow_bfs(int s){
    fill(level.begin(), level.end(), -1);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while(!que.empty()){
      int v = que.front(); que.pop();
      for(int i=0; i<G[v].size(); i++){
        edge &e = G[v][i];
        if(e.cap>0 && level[e.to]<0){
          level[e.to] = level[v] + 1;
          que.push(e.to);
        }
      }
    }
  }
  int max_flow_dfs(int v, int t, ll f){
    if(v==t) return f;
    for(int &i=iter[v]; i<G[v].size(); i++){
      edge &e = G[v][i];
      if(e.cap>0 && level[v]<level[e.to]){
        ll d = max_flow_dfs(e.to, t, min(f, e.cap));
        if(d>0){
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          e.flow += d;
          return d;
        }
      }
    }
    return 0;
  }

public:
  Dinic(int N):MAX_V(N),G(N),level(N),iter(N){
  }

  void add_edge(int from, int to, ll cap){
    G[from].push_back((edge){to, cap, (int)G[to].size(), cap, 0});
    G[to].push_back((edge){from, 0, (int)G[from].size()-1, 0, 0});
  }

  int get_flow(int from, int to){ //untried
    rep(i,G[from].size()){
      if(G[from][i].to == to){
        return G[from][i].flow;
      }
    }
    return -1;
  }

  ll max_flow(int s, int t){
    ll flow = 0;
    while(true){
      max_flow_bfs(s);
      if(level[t]<0) return flow;
      fill(iter.begin(), iter.end(), 0);
      ll f;
      while((f = max_flow_dfs(s, t, INF))>0){
        flow += f;
      }
    }
  }
  
  int min_cut(int s, int t, vector<int>& S, vector<int>& T){
    S.clear();
    T.clear();

    int maxf = max_flow(s, t);
    for(int i=0; i<level.size(); i++){
      if(level[i] >= 0) S.push_back(i);
      else T.push_back(i);
    }

    return maxf;
  }
};

int N, M;
ll d;
vector<vector<ll>> air;


int main(){
  cin >> N >> M >> d;
  rep(i,M){
    ll u, v, p, q, w;
    cin >> u >> v >> p >> q >> w;
    air.push_back({u,v,p,q,w});
  }

  map<pair<int,ll>,int> memo;
  
  vector<set<int>> G(N+1);
  rep(i,air.size()){
    G[air[i][0]].insert(air[i][2]);
    G[air[i][1]].insert(air[i][3]);
  }
  int id = 0;
  rep(i,G.size()){
    FOR(it,G[i]){
      memo[make_pair(i,*it)] = id++;
    }
  }

  Dinic dinic(id+2);
  rep(i,G.size()){
    FOR(it1,G[i]){
      FOR(it2,G[i]){
        int prev = *it1;
        int next = *it2;
        if(next-prev>=d){
          dinic.add_edge(memo[make_pair(i,prev)], memo[make_pair(i,next)], INF);
        }
      }
    }
  }
  
  rep(i,air.size()){
    dinic.add_edge(memo[make_pair(air[i][0],air[i][2])], memo[make_pair(air[i][1],air[i][3])], air[i][4]);
  }

  rep(i,G.size()){
    FOR(it,G[i]){
      if(i==1) dinic.add_edge(id, memo[make_pair(i,*it)], INF);
      if(i==N) dinic.add_edge(memo[make_pair(i,*it)], id+1, INF);
    }
  }
  cout << dinic.max_flow(id, id+1) << endl;

  return 0;
}
0