結果

問題 No.654 Air E869120
ユーザー hashiryohashiryo
提出日時 2019-06-13 14:30:16
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 2,882 bytes
コンパイル時間 2,534 ms
コンパイル使用メモリ 196,732 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 12:03:47
合計ジャッジ時間 3,491 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>

#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge] << '\n'
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
const ll INF = LLONG_MAX/2;
const ll MOD = 1e9+7;

struct FordFulkerson {
    typedef long long flow_type;
    struct edge {
        int src, dst;
        flow_type capacity, flow;
        size_t rev;
    };
    int n;
    vector<vector<edge>> adj;
    FordFulkerson(int n) : n(n), adj(n) { }
    void add_edge(int src, int dst, flow_type capacity) {
        adj[src].push_back({src, dst, capacity, 0, adj[dst].size()});
        adj[dst].push_back({dst, src, 0, 0, adj[src].size()-1});
    }
    flow_type max_flow(int s, int t) {
        vector<bool> visited(n);
        function<flow_type(int,flow_type)> augment = [&](int u, flow_type cur) {
            if (u == t) return cur;
            visited[u] = true;
            for (auto &e: adj[u]) {
                if (!visited[e.dst] && e.capacity > e.flow) {
                    flow_type f = augment(e.dst, min(e.capacity - e.flow, cur));
                    if (f > 0) {
                        e.flow += f;
                        adj[e.dst][e.rev].flow -= f;
                        return f;
                    }
                }
            }
            return flow_type(0);
        };
        for (int u = 0; u < n; ++u)
            for (auto &e: adj[u]) e.flow = 0;

        flow_type flow = 0;
        while (1) {
            fill(visited.begin(),visited.end(), false);
            flow_type f = augment(s, INF);
            if (f == 0) break;
            flow += f;
        }
        return flow;
    }
};


signed main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N,M,d;cin>>N>>M>>d;
  vll ti[N];
  vector<tuple<ll,ll,ll,ll,ll>> query(M);
  map<tuple<ll,ll>,ll> mp;
  int cnt=0;
  mp[make_tuple(0,0)]=cnt++;
  ti[0].push_back(0);
  for(ll i=0;i<M;i++){
    ll u,v,p,q,w;cin>>u>>v>>p>>q>>w;u--,v--;q+=d;
    ti[u].push_back(p);
    if(mp.count(make_tuple(u,p))==0)mp[make_tuple(u,p)]=cnt++;
    ti[v].push_back(q);
    if(mp.count(make_tuple(v,q))==0)mp[make_tuple(v,q)]=cnt++;
    query[i]=make_tuple(u,v,p,q,w);
  }
  if(mp.count(make_tuple(N-1,1e10))==0)mp[make_tuple(N-1,1e10)]=cnt++;
  ti[N-1].push_back(1e10);
  FordFulkerson g(cnt);
  for(ll i=0;i<N;i++){
    sort(ti[i].begin(),ti[i].end());
    ti[i].erase(unique(ti[i].begin(),ti[i].end()),ti[i].end());
    for(ll j=0;j+1<(ll)ti[i].size();j++){
      g.add_edge(mp[make_tuple(i,ti[i][j])],mp[make_tuple(i,ti[i][j+1])],INF);
    }
  }
  for(ll i=0;i<M;i++){
    ll u,v,p,q,w;tie(u,v,p,q,w)=query[i];
    g.add_edge(mp[make_tuple(u,p)],mp[make_tuple(v,q)],w);
  }
  cout<<g.max_flow(mp[make_tuple(0,0)],mp[make_tuple(N-1,1e10)])<<endl;
  return 0;
}
0