結果

問題 No.119 旅行のツアーの問題
ユーザー get_tanniget_tanni
提出日時 2024-07-25 13:06:35
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 5,062 bytes
コンパイル時間 9,307 ms
コンパイル使用メモリ 351,752 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-25 13:06:46
合計ジャッジ時間 10,874 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 3 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 3 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <atcoder/all>

using std::cout; using namespace std; using ll=long long; using ld=long double; using P=array<ll,2>; using mint = atcoder::modint998244353;
#define rep(i,n) for (ll i=0,siz=(n);i<siz;i++)
#define rep2(i,a,b) for (ll i=(a),siz=(b);i<siz;i++)
#define repd(i,a,b) for (ll i=(a),siz=(b);i>=siz;i--)
#define popcount __builtin_popcountll
#define cin(a) ll a; cin >> a;
#define cin2(a,b) ll a,b; cin >> a >> b;
#define cin3(a,b,c) ll a,b,c; cin >> a >> b >> c;
#define cinvec(v) vector<ll> v(N); rep(i,N) cin >> v[i];
#define cinvec2(v,n) vector<ll> v(n); rep(i,n) cin >> v[i];
#define cins(s) string s; cin >> s;
#define cinc(c) char c; cin >> c;
vector<ll> dx = {0,-1,0,1},dy = {1,0,-1,0}, ddx = {0,-1,-1,-1,0,1,1,1}, ddy = {1,1,0,-1,-1,-1,0,1};
void yesno(bool b) {cout << (b?"Yes":"No") << endl;}
template<class T> void sortunique(vector<T> &V) {sort(V.begin(), V.end()); V.erase(unique(V.begin(), V.end()), V.end());}
template<typename T> void outvec(const std::initializer_list<T>& list) { bool first = true; for (const auto& elem : list) 
{ if (!first) std::cout << " "; std::cout << elem; first = false; } std::cout << std::endl; }
template<typename T> void outvec(const std::vector<T>& vec) { bool first = true; for (const auto& elem : vec) 
{ if (!first) std::cout << " "; std::cout << elem; first = false; } std::cout << std::endl; }
template<typename T, size_t N> void outvecp(const std::vector<std::array<T,N>>& vec) { bool first = true; for (const auto& elem : vec) 
{ if (!first) std::cout << " "; for (int i = 0; i < N; i++) std::cout << elem[i] << " "; first = false; } std::cout << std::endl; }
vector<ll> compress(vector<ll> &A){vector<ll> B = A; sortunique(B); rep(i,A.size()) A[i] = lower_bound(B.begin(),B.end(),A[i]) - B.begin(); return B;}
ll bs(ll l, ll r, function<bool(ll)> f) { while (r-l > 1) { ll m = (l+r)/2; if (f(m)) r = m; else l = m; } return r; }
ll msb(ll N) { assert(N>0); return 63 - __builtin_clzll(N); }

class Dinic_MaximFlow{
    private:
        struct Edge{
            ll to;
            ll cap;
            ll rev;
        };
        vector<vector<Edge>> G;
        vector<ll> level;
        vector<ll> iter;
        ll N;
        void bfs(ll s){
            for (ll i=0;i<N;i++) level[i] = -1;
            queue<ll> que;
            level[s] = 0;
            que.push(s);
            while(!que.empty()){
                ll v = que.front(); que.pop();
                for (ll i=0;i<G[v].size();i++){
                    Edge &e = G[v][i];
                    if (e.cap > 0 && level[e.to] < 0){
                        level[e.to] = level[v] + 1;
                        que.push(e.to);
                    }
                }
            }
        }
        ll dfs(ll v, ll t, ll f){
            if (v == t) return f;
            for (ll &i=iter[v];i<G[v].size();i++){
                Edge &e = G[v][i];
                if (e.cap > 0 && level[v] < level[e.to]){
                    ll d = dfs(e.to, t, min(f, e.cap));
                    if (d > 0){
                        e.cap -= d;
                        G[e.to][e.rev].cap += d;
                        return d;
                    }
                }
            }
            return 0;
        }
    public:
        Dinic_MaximFlow(ll n){
            N = n;
            G.resize(n);
            level.resize(n);
            iter.resize(n);
        }
        void add_edge(ll from, ll to, ll cap){
            G[from].push_back((Edge){to, cap, (ll)G[to].size()});
            G[to].push_back((Edge){from, 0, (ll)G[from].size()-1});
        }
        ll max_flow(ll s, ll t){
            ll flow = 0;
            while (true){
                bfs(s);
                if (level[t] < 0) return flow;
                for (ll i=0;i<N;i++) iter[i] = 0;
                ll f;
                while ((f = dfs(s, t, 1e18)) > 0){
                    flow += f;
                }
            }
        }
        vector<vector<Edge>> edges(){
            return G;
        }

        vector<P> matching_res(ll Ns){
            vector<P> ans;
            for (ll i=0;i<Ns;i++){
                for (ll j=0;j<G[i].size();j++){
                    Edge e = G[i][j];
                    if (e.cap == 0) ans.push_back(P{i,e.to});
                }
            }
            return ans;
        }
};

int main() {
    cin(N);
    vector<ll> B(N),C(N);
    rep(i,N) cin >> B[i] >> C[i];
    cin(M);
    vector<ll> D(M),E(M);
    rep(i,M) cin >> D[i] >> E[i];

    Dinic_MaximFlow dm(2*N+2);
    ll st = 2*N,goal = 2*N+1;

    rep(i,N) {
        dm.add_edge(st,i,B[i]+C[i]);
        dm.add_edge(i,i+N,1e9);
        dm.add_edge(i,goal,C[i]);
        dm.add_edge(st,i+N,B[i]);
        dm.add_edge(i+N,goal,B[i]+C[i]);
    }

    rep(i,M) {
        dm.add_edge(D[i],E[i]+N,1e9);
    }

    ll ans = 2*accumulate(C.begin(),C.end(),0LL) + 2*accumulate(B.begin(),B.end(),0LL) - dm.max_flow(st,goal);

    cout << ans << endl;
    
    return 0;
} 
0