結果

問題 No.654 Air E869120
ユーザー hirakuuuuhirakuuuu
提出日時 2023-12-31 15:40:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,077 bytes
コンパイル時間 2,617 ms
コンパイル使用メモリ 217,396 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-12-31 15:40:23
合計ジャッジ時間 4,464 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for(int i = a; i < n; i++)
#define rrep(i, a, n) for(int i = a; i >= n; i--)
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
// constexpr ll MOD = 1000000007;
constexpr ll MOD = 998244353;
constexpr int IINF = 1001001001;
constexpr ll INF = 1LL<<60;

template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}

template <class T> 
struct Dinic{
private:
    struct Edge {
        // to: 残余グラフ上での辺の行き先
        // cap: 残余グラフ上での辺の容量
        // rev: 辺 u → G[u][i].to の逆辺 G[u][i].to → u が G[G[u][i].to] の何番目に存在するか
        // これらの情報があると残余グラフの辺の容量の変更が楽になる
        int to;
        T cap;
        int rev;
    };
    int n; // 頂点数
    vector<vector<Edge>> graph; // 残余グラフの隣接表現リスト
    vector<int> level; // sからの距離(BFSで求める)
    vector<int> iter; // どこまで探索済みか
    const T inf = 1e18;

public:
    // コンストラクタ
    Dinic(int n_) : n(n_) {
        graph.resize(n);
        level.resize(n);
        iter.resize(n);
    }

    // 頂点fromからtoに向かう、上限 cap リットル/秒の辺を追加
    void add_edge(int from, int to, T cap){
        int size_from = (int)graph[from].size(); // 現時点でのG[from]の要素数
        int size_to = (int)graph[to].size(); // 現時点でのG[to]の要素数
        graph[from].push_back(Edge{to, cap, size_to});
        graph[to].push_back(Edge{from, 0, size_from});
    }

    // sからの最短距離をBFSで計算する
    void bfs(int s){
        fill(level.begin(), level.end(), -1);
        level[s] = 0;
        queue<int> que;
        que.push(s);
        while(!que.empty()){
            int v = que.front(); que.pop();
            for(int i = 0; i < (int)graph[v].size(); i++){
                Edge &e = graph[v][i];
                if(e.cap > 0 && level[e.to] < 0){
                    level[e.to] = level[v]+1;
                    que.push(e.to);
                }
            }
        }
    }

    // 深さ優先探索(Fはスタートからposに到達するパスで流せる流量の最大値)
    // 返り値は流したフローの量(流せない場合は0を返す)
    T dfs(int v, int t, T f) {
        // ゴールに到着:フローを流せる
        if(v == t) return f;  
        // 探索する
        for(int &i = iter[v]; i < (int)graph[v].size(); i++) {
            Edge &e = graph[v][i];
            if(e.cap > 0 && level[v] < level[e.to]){
                T d = dfs(e.to, t, min(f, e.cap));
                if(d > 0){
                    e.cap -= d;
                    graph[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        // 全ての辺を探索しても見つからなかった場合は0を返す
        return 0;
    }

    // 頂点 s から頂点 t までの最大フローの総流量を返す
    T max_flow(int s, int t) {
        T flow = 0;
        while(true) {
            bfs(s);
            if(level[t] < 0) return flow;
            fill(iter.begin(), iter.end(), 0);
            T f = dfs(s, t, inf);
            // フローを流せなくなったら操作終了
            if(f == 0) break;
            flow += f;
        }
        return flow;
    }
};



int main(){
    int n, m;
    ll d; cin >> n >> m >> d;
    vector<ll> u(m+1), v(m+1), p(m+1), q(m+1), w(m+1);
    rep(i, 1, m+1) cin >> u[i] >> v[i] >> p[i] >> q[i] >> w[i];
    Dinic<ll> mf(m+2);
    rep(i, 1, m+1){
        if(u[i] == 1) mf.add_edge(0, i, w[i]);
        if(v[i] == n) mf.add_edge(i, m+1, w[i]);
    }
    rep(i, 1, m+1){
        rep(j, 1, m+1){
            if(i == j) continue;
            if(v[i] == u[j] && q[i]+d <= p[j]){
                mf.add_edge(i, j, w[i]);
            }
        }
    }

    cout << mf.max_flow(0, m+1) << endl;
    
    return 0;
}
0