結果

問題 No.1324 Approximate the Matrix
ユーザー ningenMeningenMe
提出日時 2020-12-26 13:29:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 8,448 bytes
コンパイル時間 1,204 ms
コンパイル使用メモリ 103,392 KB
実行使用メモリ 9,308 KB
最終ジャッジ日時 2024-09-24 16:35:55
合計ジャッジ時間 4,421 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 87 ms
9,108 KB
testcase_04 AC 90 ms
8,324 KB
testcase_05 AC 88 ms
9,308 KB
testcase_06 AC 87 ms
8,452 KB
testcase_07 AC 87 ms
8,328 KB
testcase_08 AC 23 ms
6,940 KB
testcase_09 AC 7 ms
6,944 KB
testcase_10 AC 16 ms
6,944 KB
testcase_11 AC 36 ms
6,944 KB
testcase_12 AC 5 ms
6,944 KB
testcase_13 AC 4 ms
6,940 KB
testcase_14 AC 47 ms
7,644 KB
testcase_15 AC 23 ms
6,944 KB
testcase_16 AC 3 ms
6,940 KB
testcase_17 AC 13 ms
6,944 KB
testcase_18 AC 7 ms
6,944 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 6 ms
6,944 KB
testcase_21 AC 3 ms
6,944 KB
testcase_22 AC 15 ms
6,944 KB
testcase_23 AC 6 ms
6,944 KB
testcase_24 AC 41 ms
6,940 KB
testcase_25 AC 14 ms
6,940 KB
testcase_26 AC 15 ms
6,940 KB
testcase_27 AC 5 ms
6,940 KB
testcase_28 AC 1 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 1 ms
6,944 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 1 ms
6,944 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 2 ms
6,944 KB
testcase_37 AC 98 ms
8,448 KB
testcase_38 AC 93 ms
8,324 KB
testcase_39 AC 94 ms
8,448 KB
testcase_40 AC 92 ms
8,328 KB
testcase_41 AC 92 ms
8,456 KB
testcase_42 AC 36 ms
8,328 KB
testcase_43 AC 34 ms
8,324 KB
testcase_44 AC 34 ms
8,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <limits>
#include <queue>
#include <vector>
#include <iostream>

template<class Flow, class Cost> struct mccf_graph{

    int n;

    // input edges
    struct edge_with_function{
        int from, to;
        int64_t a,b;
        Flow cap, flow;
        int64_t F(int64_t x){return (x-a)*(x-a)*b;};
    };

    // edges on the residual graph
    struct edge{
        int to, rev, id;
        bool available;
        Cost cost;
        bool is_rev;

        // for debug
        void print_edge(){ std::cerr << "to: " << to << " cost: " << cost << "\n";}
    };

    std::vector<std::vector<edge>> g;
    std::vector<edge_with_function> E;
    std::vector<Cost> dual;

    mccf_graph() {}
    mccf_graph(int n) : n(n), g(n){
        dual.resize(n, 0);
    }

    int add_edge(int from, int to, int64_t a,int64_t b, Flow cap, Flow flow){
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        // assert(cap > 0);
        int m = int(E.size());
        E.push_back(edge_with_function{from, to, a, b, cap, flow});
        return m;
    }

    Cost calc_objective(){
        Cost obj = 0;
        for(auto &e: E){
            obj += e.F(e.flow);
        }
        return obj;
    }

    /* for debug
    void print_g(){
        for(int i=0;i<n;i++){
            for(edge &e: g[i]){
                std::cerr << "to " << e.to;
                std::cerr << " rev " << e.rev;
                std::cerr << " id " << e.id;
                std::cerr << " avail " << (int)e.available;
                std::cerr << " cost " << e.cost;
                std::cerr << " r_cost " << e.cost - dual[e.to] + dual[i];
                std::cerr << " is_rev " << (int)e.is_rev;
                std::cerr << "\n";
            }
        }
        std::cerr << "\n";
    }

    void print_E(){
        for(auto &e: E){
            std::cerr << "from " << e.from;
            std::cerr << " to " << e.to;
            std::cerr << " cap " << e.cap;
            std::cerr << " flow " << e.flow;
            std::cerr << "\n";
        }
        std::cerr << "\n";
    }
    for debug */ 

    void solve_SSP(int s, int t){
        solve_SSP(s, t, std::numeric_limits<Flow>::max());
    }

    void solve_SSP(int s, int t, Flow flow_limit){
        assert(0 <= s and s < n);
        assert(0 <= t and t < n);
        assert(s != t);

        // construct a residual graph
        int m = E.size();
        for(int i=0;i<m;i++){
            auto &e = E[i];
            if(!e.cap) continue;

            g[e.from].push_back(edge{e.to, (int)g[e.to].size(), i, true, e.F(1) - e.F(0), false});

            g[e.to].push_back(edge{e.from, (int)g[e.from].size()-1, i, false, 0, true});
        }

        // get flow   
        std::vector<Cost> dist(n, 0);
        std::vector<int> pv(n), pe(n);
        std::vector<bool> vis(n);

        auto dual_ref_Dijkstra = [&]()->bool{
            fill(dist.begin(), dist.end(), std::numeric_limits<Cost>::max());
            fill(pv.begin(), pv.end(), -1);
            fill(pe.begin(), pe.end(), -1);
            fill(vis.begin(), vis.end(), false);

            struct Q{
                Cost key;
                int to;
                bool operator<(Q r) const {return key > r.key;};
            };
            std::priority_queue<Q> que;
            dist[s] = 0;
            que.push(Q{0, s});

            while(!que.empty()){
                int v = que.top().to;
                que.pop();
                if(vis[v]) continue;
                vis[v] = true;
                if(v == t) break;
                for(int i=0;i<(int)g[v].size();i++){
                    auto &e = g[v][i];
                    if(vis[e.to] or !e.available) continue;
                    Cost cost = e.cost - dual[e.to] + dual[v];
                    if(dist[e.to] - dist[v] > cost){
                        dist[e.to] = dist[v] + cost;
                        pv[e.to] = v;
                        pe[e.to] = i;
                        que.push(Q{dist[e.to], e.to});
                    }
                }
            }

            if(!vis[t]){
                return false;
            }
            // assert(vis[t]);

            for(int v=0;v<n;v++){
                if(!vis[v]) continue;
                dual[v] -= dist[t] - dist[v];
            }

            return true;
        };

        auto dual_ref_BF = [&]()->bool{
            fill(dist.begin(), dist.end(), std::numeric_limits<Cost>::max());
            fill(pv.begin(), pv.end(), -1);
            fill(pe.begin(), pe.end(), -1);
            fill(vis.begin(), vis.end(), false);

            dist[s] = 0;
            vis[s] = true;
            for(int itr=0;itr<n-1;itr++){
                for(int v=0;v<n;v++){
                    // if(dist[v] == INF_cost) continue;
                    if(!vis[v]) continue;
                    for(int i=0;i<(int)g[v].size();i++){
                        auto &e = g[v][i];
                        if(!e.available) continue;
                        if(dist[e.to] > dist[v] + e.cost){
                            dist[e.to] = dist[v] + e.cost;
                            pv[e.to] = v;
                            pe[e.to] = i;
                            vis[e.to] = true;
                        }
                    }
                }
            }

            // check feasibility
            if(!vis[t]){
                return false;
            }

            // detect negative cycle
            for(int v=0;v<n;v++){
                // if(dist[v] == INF_cost) continue;
                if(!vis[v]) continue;
                for(auto &e: g[v]){
                    if(!e.available) continue;
                    assert(dist[v] + e.cost >= dist[e.to]);
                }
            }

            for(int v=0;v<n;v++){
                // assert(dist[v]<INF_cost);
                if(!vis[v]) continue;
                dual[v] -= dist[t] - dist[v];
            }            

            return true;
        };

        auto update_edge = [&](edge &e){
            auto &e_func = E[e.id];
            if(e.is_rev){
                if(e_func.flow == 0){
                    e.available = false;
                    e.cost = 0;
                }else{
                    e.available = true;
                    e.cost = e_func.F(e_func.flow-1) - e_func.F(e_func.flow);
                }
            }else{
                if(e_func.flow == e_func.cap){
                    e.available = false;
                    e.cost = 0;
                }else{
                    e.available = true;
                    e.cost = e_func.F(e_func.flow+1) - e_func.F(e_func.flow);
                }
            }
            return;
        };

        Flow flow_cur = 0;
        while(flow_cur < flow_limit){

            if(!flow_cur){
                if(!dual_ref_BF()) break;
            }else{
                if(!dual_ref_Dijkstra()) break;
            }

            for(int v=t;v!=s;v=pv[v]){
                auto& e = g[pv[v]][pe[v]];
                auto& e_func = E[e.id];
                auto& re = g[v][e.rev];
                if(e.is_rev) e_func.flow--;
                else e_func.flow++;
                update_edge(e);
                update_edge(re);
            }
            flow_cur++;
        }
    }
};

int main(){

    int N, M;
    std::cin >> N >> M;
    std::vector<int> A(N), B(N);
    for(int i=0;i<N;i++) std::cin >> A[i];
    for(int i=0;i<N;i++) std::cin >> B[i];
    std::vector<std::vector<int>> T(N, std::vector<int>(N, 0));
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            std::cin >> T[i][j];
        }
    }

    mccf_graph<int64_t, int64_t> G(2*N+2);
    int s = 2*N;
    int t = s+1;
    int INF_cap = 1e9;

    for(int i=0;i<N;i++){
        auto F = [](int64_t x){return 0;}; // コスト関数の定義
        G.add_edge(s, i, 0, 0, A[i], 0); // 辺を張る
    }

    for(int i=0;i<N;i++){
        auto F = [](int64_t x){return 0;}; // コスト関数の定義
        G.add_edge(N+i, t, 0, 0, B[i], 0); // 辺を張る
    }

    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            auto F = [&T, i, j](int64_t x){return (x-T[i][j])*(x-T[i][j]);}; // コスト関数の定義
            G.add_edge(i, N+j, T[i][j], 1, INF_cap, 0); // 辺を張る
        }
    }

    G.solve_SSP(s, t, M); // フローを流す

    std::cout << G.calc_objective() << "\n"; // 目的関数値を出力

    return 0;
}
0