結果

問題 No.654 Air E869120
ユーザー hirakuuuuhirakuuuu
提出日時 2023-12-31 15:54:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 3,852 bytes
コンパイル時間 2,645 ms
コンパイル使用メモリ 215,244 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-12-31 15:54:31
合計ジャッジ時間 4,477 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 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 89 ms
6,676 KB
testcase_11 AC 39 ms
6,676 KB
testcase_12 AC 53 ms
6,676 KB
testcase_13 AC 61 ms
6,676 KB
testcase_14 AC 22 ms
6,676 KB
testcase_15 AC 31 ms
6,676 KB
testcase_16 AC 9 ms
6,676 KB
testcase_17 AC 11 ms
6,676 KB
testcase_18 AC 8 ms
6,676 KB
testcase_19 AC 11 ms
6,676 KB
testcase_20 AC 6 ms
6,676 KB
testcase_21 AC 6 ms
6,676 KB
testcase_22 AC 6 ms
6,676 KB
testcase_23 AC 6 ms
6,676 KB
testcase_24 AC 6 ms
6,676 KB
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 6 ms
6,676 KB
testcase_30 AC 5 ms
6,676 KB
testcase_31 AC 4 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 2 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 FordFulkerson{
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<bool> used; // 頂点が探索済みであるかをチェックするリスト
    vector<vector<Edge>> graph; // 残余グラフの隣接表現リスト
public:
    FordFulkerson(int n_) : n(n_) {
        used.resize(n);
        graph.resize(n);
    }

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

    // 深さ優先探索(Fはスタートからposに到達するパスで流せる流量の最大値)
    // 返り値は流したフローの量(流せない場合は0を返す)
    T dfs(int pos, int goal, T F) {
        // ゴールに到着:フローを流せる
        if(pos == goal) return F;  

        used[pos] = true;  // posを探索済みとする

        // 探索する
        rep(i, 0, (int)graph[pos].size()) {
            // 容量0ならば流さない
            if(graph[pos][i].cap == 0) continue;
            // すでに探索済みの頂点にも流さない
            if(used[graph[pos][i].to] == true) continue;
            // 目的地までのパスを探す
            T flow = dfs(graph[pos][i].to, goal, min(F, graph[pos][i].cap));

            // フローを流せる場合、残余グラフの容量を flow だけ増減させる
            if(flow > 0) {
                graph[pos][i].cap -= flow;
                graph[graph[pos][i].to][graph[pos][i].rev].cap += flow;
                return flow;
            }
        }

        // 全ての辺を探索しても見つからなかった場合は0を返す
        return 0;
    }

    // 頂点 s から頂点 t までの最大フローの総流量を返す
    T max_flow(int s, int t) {
        T total_flow = 0;
        while(true) {
            rep(i, 0, n) used[i] = false;
            T F = dfs(s, t, 1e9);

            // フローを流せなくなったら操作終了
            if(F == 0) break;
            total_flow += F;
        }
        return total_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];
    FordFulkerson<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, min(w[i], w[j]));
            }
        }
    }

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