結果

問題 No.904 サメトロ
ユーザー mugen_1337mugen_1337
提出日時 2020-04-30 09:34:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 3,451 bytes
コンパイル時間 2,829 ms
コンパイル使用メモリ 210,648 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-21 05:20:51
合計ジャッジ時間 4,264 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define INF 1000000000
#define mod 1000000007
using ll=long long;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

template<typename T>
struct Dinic{
    //適当にフローを流す
    //流した辺のcapを減らし,逆辺のcapをその分増やす
    //これを繰り返す
    //O(EV^2),通常こんなにかからない
 
    const T TINF;
 
    struct edge{
        int to;
        //容量
        T cap;
        int rev;
        bool isrev;
        int idx;
    };
 
    vector<vector<edge>> g;
    vector<int> min_cost, iter;
 
    Dinic(int V):TINF(numeric_limits<T>::max()),g(V){}
 
    void add_edge(int from, int to, T cap, int idx=-1){
        g[from].push_back((edge){to,   cap, (int)g[to].size(),     false, idx});
        g[to].  push_back((edge){from, 0,   (int)g[from].size()-1, true,  idx});
    }
 
    bool bfs(int s, int t){
        min_cost.assign(g.size(), -1);
        queue<int> q;
        min_cost[s]=0;
        q.push(s);
        while(!q.empty() and min_cost[t]==-1){
            int p=q.front();q.pop();
            for(auto &e:g[p]){
                //流せるなら
                if(e.cap>0 and min_cost[e.to]==-1){
                    min_cost[e.to]=min_cost[p]+1;
                    q.push(e.to);
                }
            }
        }
        //tまで流せるパスが1つでもあるならtrueを返す
        return min_cost[t]!=-1;
    }
 
    //dfsでtまでフローを流せるだけ流すdfs
    T dfs(int idx, const int t, T flow){
        if(idx==t) return flow;
        for(int &i=iter[idx];i<g[idx].size();i++){
            edge &e=g[idx][i];
            if(e.cap and min_cost[idx]<min_cost[e.to]){
                //今のポイントに流れてきた分を次の点に流せるだけ流す
                T d=dfs(e.to, t, min(flow,e.cap));
                if(d>0){
                    //流せるならcapの量を減らし,逆辺のcapを増やす
                    e.cap-=d;
                    g[e.to][e.rev].cap+=d;
                    //どこか1つ見つければいいのでd>0で
                    //dが見つかれば即リターン
                    //次はi以降を観ればいいので&iでiを更新してiterに記憶
                    return d;
                }
            }
        }
        return 0;
    }
 
    T max_flow(int s,int t){
        T flow=0;
        while(bfs(s,t)){
            iter.assign(g.size(),0);
            T f=0;
            while((f=dfs(s,t,INF))>0) flow+=f;
        }
        return flow;
    }
 
};

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int n;cin>>n;n--;
    ll a[n],b[n],sum=0;
    rep(i,n){
        cin>>a[i]>>b[i];
        sum+=a[i];
    }
    int st=2*n,gl=2*n+1;
    Dinic<int> flow(2*n+2);
    rep(i,n){
        flow.add_edge(st,i,a[i]);
        flow.add_edge(i+n,gl,b[i]);
        rep(j,n)if(i!=j){
            flow.add_edge(i,j+n,INF);
        }
    }
    int sabakenai=sum-flow.max_flow(st,gl);
    cout<<sum-sabakenai+1<<endl;
    return 0;
}
0