結果

問題 No.1324 Approximate the Matrix
ユーザー theory_and_metheory_and_me
提出日時 2020-12-26 12:13:01
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 8,428 bytes
コンパイル時間 986 ms
コンパイル使用メモリ 80,084 KB
最終ジャッジ日時 2023-10-24 20:20:41
合計ジャッジ時間 2,440 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp:15:14: error: 'function' in namespace 'std' does not name a template type
   15 |         std::function<Cost(Flow)> F; // must be discrete convex
      |              ^~~~~~~~
main.cpp:7:1: note: 'std::function' is defined in header '<functional>'; did you forget to '#include <functional>'?
    6 | #include <iostream>
  +++ |+#include <functional>
    7 | 
main.cpp:39:36: error: 'std::function' has not been declared
   39 |     int add_edge(int from, int to, std::function<Cost(Flow)> F, Flow cap, Flow flow){
      |                                    ^~~
main.cpp:39:49: error: expected ',' or '...' before '<' token
   39 |     int add_edge(int from, int to, std::function<Cost(Flow)> F, Flow cap, Flow flow){
      |                                                 ^
main.cpp: In member function 'int mccf_graph<Flow, Cost>::add_edge(int, int, int)':
main.cpp:44:50: error: 'F' was not declared in this scope
   44 |         E.push_back(edge_with_function{from, to, F, cap, flow});
      |                                                  ^
main.cpp:44:53: error: 'cap' was not declared in this scope
   44 |         E.push_back(edge_with_function{from, to, F, cap, flow});
      |                                                     ^~~
main.cpp:44:58: error: 'flow' was not declared in this scope; did you mean 'Flow'?
   44 |         E.push_back(edge_with_function{from, to, F, cap, flow});
      |                                                          ^~~~
      |                                                          Flow
main.cpp: In function 'int main()':
main.cpp:272:19: error: no matching function for call to 'mccf_graph<long int, long int>::add_edge(int&, int&, main()::<lambda(int64_t)>&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&, int)'
  272 |         G.add_edge(s, i, F, A[i], 0); // 辺を張る
      |         ~~~~~~~~~~^~~~~~~~~~~~~~~~~~
main.cpp:39:9: note: candidate: 'int mccf_graph<Flow, Cost>::add_edge(int, int, int) [with Flow

ソースコード

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;
        std::function<Cost(Flow)> F; // must be discrete convex
        Flow cap, flow;
    };

    // 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, std::function<Cost(Flow)> F, 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, F, 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, F, A[i], 0); // 辺を張る
    }

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

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

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

    return 0;
}
0