結果

問題 No.119 旅行のツアーの問題
ユーザー GOTKAKO
提出日時 2025-07-12 06:19:17
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,964 bytes
コンパイル時間 2,596 ms
コンパイル使用メモリ 213,636 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-07-12 06:19:21
合計ジャッジ時間 3,592 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

long long inf = 2e18;
struct edge{ int to; long long cap; int rev; bool isrev;};
class Dinic{
public:
    int siz = 0;
    vector<vector<edge>> Graph;
    vector<int> searched,dist;
    vector<bool> visited;
 
    void make(int N){
        siz = N;
        Graph.resize(N); visited.resize(N);
        dist.resize(N,-1);
    }
 
    void addedge(int u, int v, long long c){
        int gv = Graph.at(v).size(), gu = Graph.at(u).size();
        Graph.at(u).push_back({v,c,gv,false});
        Graph.at(v).push_back({u,0,gu,true});
    }
 
    void bfs(int s, int t){
        queue<int> Q; Q.push(s);
        dist.at(s) = 0;
        while(Q.size()){
            int pos = Q.front(); Q.pop();
            for(auto &[to,c,r,ign] : Graph.at(pos)){
                if(c > 0 && dist.at(to) == -1){
                    dist.at(to) = dist.at(pos)+1;
                    if(to != t) Q.push(to);
                }
            }
        }
    }
 
    long long dfs(int pos, int start, long long f){
        if(pos == start) return f;
        visited.at(pos) = true;
 
        long long flow = 0;
        for(auto &[t,c,r,ign] : Graph.at(pos)){
            long long &revc = Graph.at(t).at(r).cap;
            if(visited.at(t) || revc == 0 || f-flow == 0 || dist.at(pos) <= dist.at(t)) continue;
            long long now = dfs(t,start,min(revc,f-flow));
            c += now; revc -= now;
 
            flow += now;
        }
        return flow;
    }
 
    long long maxflow(int s, int t){
        long long ret = 0;
        while(true){
            visited.assign(siz,false);
            dist.assign(siz,-1);
            bfs(s,t);
            if(dist.at(t) == -1) break;
            ret += dfs(t,s,inf);
        }
        return ret;
    }
 
    vector<pair<int,int>> mincost(int s){
        //maxflowの後の復元
        vector<int> distS(siz,-1);
        distS.at(s) = 1;
        queue<int> Q; Q.push(s);
        while(Q.size()){
            int pos = Q.front(); Q.pop();
            for(auto &[to,c,r,ign] : Graph.at(pos)){
                if(c == 0 || distS.at(to) != -1) continue;
                distS.at(to) = 1; Q.push(to);
            }
        }
 
        vector<pair<int,int>> ret;
        for(int i=0; i<siz; i++){
            for(auto &[to,c,r,isr] : Graph.at(i)){
                if(isr) continue;
                if(distS.at(i) == 1 && distS.at(to) == -1) ret.push_back({i,to});
            }
        }
        return ret;
    }
};

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N; cin >> N;
    Dinic Z; Z.make(N+N+2);
    int s = N+N,t = s+1;
    
    int answer = 0;
    for(int i=0; i<N; i++){
        int b,c; cin >> b >> c;
        answer += b+c;
        Z.addedge(s,i,c),Z.addedge(i,i+N,b+c),Z.addedge(i+N,t,b);
    }
    int M; cin >> M;
    while(M--){
        int d,e; cin >> d >> e; 
        Z.addedge(e+N,d,inf);
    }
    cout << answer-Z.maxflow(s,t) << endl;
}
0