結果

問題 No.2604 Initial Motion
ユーザー umimelumimel
提出日時 2024-01-12 22:35:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 944 ms / 3,000 ms
コード長 6,299 bytes
コンパイル時間 2,148 ms
コンパイル使用メモリ 186,736 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2024-01-12 22:36:13
合計ジャッジ時間 20,184 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 26 ms
6,548 KB
testcase_04 AC 26 ms
6,548 KB
testcase_05 AC 25 ms
6,548 KB
testcase_06 AC 26 ms
6,548 KB
testcase_07 AC 26 ms
6,548 KB
testcase_08 AC 25 ms
6,548 KB
testcase_09 AC 27 ms
6,548 KB
testcase_10 AC 27 ms
6,548 KB
testcase_11 AC 27 ms
6,548 KB
testcase_12 AC 27 ms
6,548 KB
testcase_13 AC 803 ms
6,548 KB
testcase_14 AC 581 ms
6,548 KB
testcase_15 AC 305 ms
6,548 KB
testcase_16 AC 770 ms
6,548 KB
testcase_17 AC 944 ms
6,548 KB
testcase_18 AC 930 ms
6,548 KB
testcase_19 AC 901 ms
6,548 KB
testcase_20 AC 718 ms
6,548 KB
testcase_21 AC 611 ms
6,548 KB
testcase_22 AC 899 ms
6,548 KB
testcase_23 AC 622 ms
6,548 KB
testcase_24 AC 796 ms
6,548 KB
testcase_25 AC 920 ms
6,548 KB
testcase_26 AC 676 ms
6,548 KB
testcase_27 AC 510 ms
6,548 KB
testcase_28 AC 642 ms
6,548 KB
testcase_29 AC 822 ms
6,548 KB
testcase_30 AC 542 ms
6,548 KB
testcase_31 AC 710 ms
6,548 KB
testcase_32 AC 620 ms
6,548 KB
testcase_33 AC 155 ms
6,548 KB
testcase_34 AC 422 ms
6,548 KB
testcase_35 AC 422 ms
6,548 KB
testcase_36 AC 394 ms
6,548 KB
testcase_37 AC 189 ms
6,548 KB
testcase_38 AC 2 ms
6,548 KB
testcase_39 AC 3 ms
6,548 KB
testcase_40 AC 276 ms
6,548 KB
testcase_41 AC 271 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[3] = {999727999, 1070777777, 1000000007};
const ll LINF = 1LL << 60LL;
const int IINF = (1 << 30) - 1;


template<typename T> struct Edge{
    int to; T w;
    Edge(int to_, T w_=1){
        to = to_;
        w=w_;
    }
};
template<typename T> using Tree = vector<vector<Edge<T>>>;
template<typename T> using Graph = vector<vector<Edge<T>>>;
/* 容量&重み付きエッジ for Dinic */
template<typename T> struct REdge{
    int to;
    T cap;
    T cost;
    int rev;
    REdge(int to_, T cap_, T cost_=1){
        to = to_;
        cap = cap_;
        cost = cost_;
    }
    
    REdge(int to_, T cap_, T cost_, int rev_){
        to = to_;
        cap = cap_;
        cost = cost_;
        rev = rev_;
    }
};

/* 残余グラフ for Dinic */
template<typename T> using RGraph = vector<vector<REdge<T>>>;

// ここから
template<typename T>
struct MinCostFlow{
    vector<vector<REdge<T>>> graph;
    int n;

    MinCostFlow(RGraph<T> graph_){
        n = (int)graph_.size();
        graph.resize(n);

        for(int from=0; from<n; from++){
            for(REdge<T> e : graph_[from]){
                graph[from].push_back(REdge<T>(e.to, e.cap, e.cost, (int)graph[e.to].size()));
                graph[e.to].pb(REdge<T>(from, 0, -e.cost, (int)graph[from].size()-1));
            }
        }
    }

    //コストが非負実数の場合
    T min_cost_flow1(int s, int t, T f){
        RGraph<T> rgraph(n);
        vector<T> h(n, 0); //ポテンシャル
        vector<T> dist(n, 0); //最短距離
        vector<int> prevv(n, 0); // 直前の頂点
        vector<int> preve(n, 0); // 直前の辺

        for(int from=0; from<n; from++) for(REdge<T> e : graph[from]) rgraph[from].push_back(e);

        T res = 0;
        while(f > 0){
            //ダイクストラ法を用いてhを更新
            priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> PQ;
            for(int i=0; i<n; i++) dist[i] = LINF;
            dist[s] = 0;
            PQ.push({0, s});
            while(!PQ.empty()){
                pair<T, int> p = PQ.top();
                PQ.pop();
                int v = p.se;
                if(dist[v] < p.fi) continue;

                for(int i=0; i<(int)rgraph[v].size(); i++){
                    REdge<T> &e = rgraph[v][i];
                    if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){
                        dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
                        prevv[e.to] = v;
                        preve[e.to] = i;
                        PQ.push({dist[e.to], e.to});
                    }
                }
            }
            if(dist[t] == LINF){
                //これ以上流せない
                return -1;
            }
            for(int v=0; v<n; v++) h[v] += dist[v];

            // s-t間最短経路に沿って目一杯流す
            T d = f;
            for(int v=t; v!=s; v=prevv[v]){
                d = min(d, rgraph[prevv[v]][preve[v]].cap);
            }
            f -= d;
            res += d*h[t];
            for(int v=t; v!=s; v=prevv[v]){
                REdge<T> &e = rgraph[prevv[v]][preve[v]];
                e.cap -= d;
                rgraph[v][e.rev].cap += d;
            }
        }

        return res;
    }

    //負のコストが存在する場合
    T min_cost_flow2(int s, int t, T f){
        RGraph<T> rgraph(n);
        vector<T> dist(n, 0); //最短距離
        vector<int> prevv(n, 0); // 直前の頂点
        vector<int> preve(n, 0); // 直前の辺

        for(int from=0; from<n; from++) for(REdge<T> e : graph[from]) rgraph[from].push_back(e);

        T res = 0;
        while(f > 0){
            //ベルマンフォード法により, s-t間最短経路を求める
            for(int i=0; i<n; i++) dist[i] = LINF;
            dist[s] = 0;
            bool update = true;
            while(update){
                update = false;
                for(int v=0; v<n; v++){
                    if(dist[v]==LINF) continue;
                    for(int i=0; i<(int)rgraph[v].size(); i++){
                        REdge<T> &e = rgraph[v][i];
                        if(e.cap > 0 && dist[e.to] > dist[v] + e.cost){
                            dist[e.to] = dist[v] + e.cost;
                            prevv[e.to] = v;
                            preve[e.to] = i;
                            update = true;
                        }
                    }
                }
            }

            if(dist[t] == LINF){
                //これ以上流せない
                return -1;
            }

            //s-t間最短経路に沿って目一杯流す
            T d = f;
            for(int v=t; v!=s; v=prevv[v]){
                d = min(d, rgraph[prevv[v]][preve[v]].cap);
            }
            f -= d;
            res += d*dist[t];
            for(int v=t; v!=s; v=prevv[v]){
                REdge<T> &e = rgraph[prevv[v]][preve[v]];
                e.cap -= d;
                rgraph[v][e.rev].cap += d;
            }
        }

        return res;
    }
};

void solve(){
    int K, N, M; cin >> K >> N >> M;
    vector<int> A(K);
    for(int i=0; i<K; i++){
        cin >> A[i];
        A[i]--;
    }
    vector<int> B(N);
    for(int i=0; i<N; i++){
        cin >> B[i];
    }

    RGraph<ll> G(N+2);
    int s = N, t = N+1;
    for(int i=0; i<K; i++) G[s].pb(REdge<ll>(A[i], 1, 0LL));
    for(int i=0; i<M; i++){
        int u, v;
        ll d;
        cin >> u >> v >> d;
        u--; v--;
        G[u].pb(REdge<ll>(v, LINF, d));
        G[v].pb(REdge<ll>(u, LINF, d));
    }
    for(int i=0; i<N; i++) G[i].pb(REdge<ll>(t, B[i], 0LL));

    MinCostFlow<ll> mcf(G);
    cout << mcf.min_cost_flow1(s, t, K) << '\n';
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int T=1;
    //cin >> T;
    while(T--) solve();
}
0