結果

問題 No.1324 Approximate the Matrix
ユーザー theory_and_metheory_and_me
提出日時 2021-07-16 01:40:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 9,521 bytes
コンパイル時間 2,645 ms
コンパイル使用メモリ 227,284 KB
実行使用メモリ 8,188 KB
最終ジャッジ日時 2024-07-05 03:27:54
合計ジャッジ時間 6,704 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 132 ms
7,952 KB
testcase_04 AC 138 ms
7,968 KB
testcase_05 AC 137 ms
7,976 KB
testcase_06 AC 134 ms
8,036 KB
testcase_07 AC 144 ms
8,188 KB
testcase_08 AC 18 ms
5,376 KB
testcase_09 AC 14 ms
5,376 KB
testcase_10 AC 31 ms
5,376 KB
testcase_11 AC 49 ms
5,376 KB
testcase_12 AC 12 ms
5,376 KB
testcase_13 AC 8 ms
5,376 KB
testcase_14 AC 54 ms
5,488 KB
testcase_15 AC 25 ms
5,376 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 31 ms
5,376 KB
testcase_18 AC 13 ms
5,376 KB
testcase_19 AC 10 ms
5,376 KB
testcase_20 AC 9 ms
5,376 KB
testcase_21 AC 6 ms
5,376 KB
testcase_22 AC 7 ms
5,376 KB
testcase_23 AC 25 ms
5,376 KB
testcase_24 AC 69 ms
6,288 KB
testcase_25 AC 36 ms
5,376 KB
testcase_26 AC 35 ms
5,376 KB
testcase_27 AC 18 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 134 ms
7,968 KB
testcase_38 AC 134 ms
7,976 KB
testcase_39 AC 135 ms
8,088 KB
testcase_40 AC 137 ms
7,976 KB
testcase_41 AC 138 ms
7,964 KB
testcase_42 AC 6 ms
5,376 KB
testcase_43 AC 6 ms
5,376 KB
testcase_44 AC 6 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x)  cerr << "Line " << __LINE__ << ": " <<  #x << " = " << (x) << "\n";
#define spa << " " <<
#define fi first
#define se second
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()

using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;

template<typename T> using V = vector<T>;
template<typename T> using P = pair<T, T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;

template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());}
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<"\n";};

const ll INF = (1ll<<62);
// const ld EPS   = 1e-10;
// const ld PI    = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;

#include <atcoder/maxflow>
using namespace atcoder;
template < class Flow, class Cost > struct cs_graph{

    struct edge{
        int from, to;
        Flow cap, flow;
        Cost cost, cost_m;
    };

    struct residual_edge{
        int to, rev, id;
        bool is_rev;
        Flow cap;
        Cost cost;
    };

    int n;
    vector<edge> E;
    vector<Cost> dual;
    vector<Flow> b;

    cs_graph() {}
    cs_graph(int n) : n(n){
        dual.resize(n, 0);
        b.resize(n, 0);
    }

    int add_edge(int from, int to, Flow cap, Flow flow, Cost cost){
        int m = int(E.size());
        E.push_back(edge{from, to, cap, flow, cost, -1});   
        return m;
    }

    void set_b(const vector<Flow> &b_input){
        b = b_input;
        return;
    }

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

    void print_E(){
        for(const auto &e: E){
            cerr << "from: " << e.from << ", to: " << e.to <<  ", cap: " << e.cap << ", flow: " << e.flow << ", cost: " << e.cost << ", cost_m: " << e.cost_m << "\n" ;
        }
        return;
    }

    bool check_feasibility(){
        
        mf_graph<Flow> mf_G(n+2);
        int s = n;
        int t = s + 1;

        Flow plus_flow_sum = 0, minus_flow_sum = 0;

        for(int i=0;i<n;i++){
            if(b[i] > 0){
                plus_flow_sum += b[i];
                mf_G.add_edge(s, i, b[i]);
            }
            if(b[i] < 0){
                minus_flow_sum += (-b[i]);
                mf_G.add_edge(i, t, -b[i]);
            }
        }

        assert(plus_flow_sum == minus_flow_sum);

        for(const auto &e: E){
            mf_G.add_edge(e.from, e.to, e.cap);
        }

        Flow mf = mf_G.flow(s, t);

        return (mf == plus_flow_sum);
    }

    Cost cost_scaling(){
        Cost cost_max = 0;
        for(const auto &e: E){
            if(e.cost > cost_max) cost_max = e.cost;
        }

        Cost scaling_factor = 2;
        
        Cost init_eps = 1;
        while(true){
            init_eps *= scaling_factor;
            if(init_eps >= n * cost_max) break;
        }

        for(auto &e: E){
            e.cost_m = e.cost * init_eps;
        }

        Cost eps = init_eps;

        while(true){

            // improve approximation(eps, x, pi)
            vector<vector<residual_edge>> g(n);

            // for debug
            // auto print_residual_graph = [&](){
            //     for(int i=0;i<n;i++){
            //         for(int j=0;j<(int)g[i].size();j++){
            //             const auto &e = g[i][j];
            //             Cost reduced_cost = e.cost - dual[i] + dual[e.to];
            //             if(e.cap > 0) cerr << "from: " << i << ", to: " << e.to << ", cap: " << e.cap <<  ", r_cost: " << reduced_cost << "\n";
            //         }
            //     }
            // };

            // add edges to the residual graph
            for(int i=0;i<(int)E.size();i++){
                auto &e = E[i];
                Cost reduced_cost = e.cost_m - dual[e.from] + dual[e.to];
                if(reduced_cost > 0){
                    e.flow = 0;
                    g[e.from].push_back(residual_edge{e.to, (int)g[e.to].size(), i, false, e.cap, e.cost_m});
                    g[e.to].push_back(residual_edge{e.from, (int)g[e.from].size() - 1, i, true, 0, -e.cost_m});
                }else{
                    e.flow = e.cap;
                    g[e.from].push_back(residual_edge{e.to, (int)g[e.to].size(), i, false, 0, e.cost_m});
                    g[e.to].push_back(residual_edge{e.from, (int)g[e.from].size() - 1, i, true, e.cap, -e.cost_m});
                }
            }

            // calculate node balance
            vector<Flow> balance = b;
            for(const auto &e: E){
                balance[e.from] -= e.flow;
                balance[e.to] += e.flow;
            }

            // define active node queue
            queue<int> active;
            for(int i=0;i<n;i++){
                if(balance[i] > 0){
                    active.push(i);
                }
            }

            // print_residual_graph();

            vector<int> current_arc(n, 0);
            vector<bool> is_positive_cap(n, false);

            while(!active.empty()){
                int v = active.front();
                while(true){
                    if(current_arc[v] == (int)g[v].size()){
                        if(!is_positive_cap[v]){
                            return -1;
                        }
                        dual[v] += eps / scaling_factor;
                        current_arc[v] = 0;
                        is_positive_cap[v] = false;
                    }else{
                        auto &e = g[v][current_arc[v]];
                        Cost reduced_cost = e.cost - dual[v] + dual[e.to];
                        if(e.cap > 0){
                            is_positive_cap[v] = true;
                        }
                        if(e.cap > 0 and reduced_cost < 0){
                            Flow push_flow = min(balance[v], e.cap);
                            Flow pre_balance = balance[e.to];

                            // augment
                            e.cap -= push_flow;
                            g[e.to][e.rev].cap += push_flow;
                            balance[v] -= push_flow;
                            balance[e.to] += push_flow;

                            // change flow of E
                            if(!e.is_rev){
                                E[e.id].flow += push_flow;
                            }else{
                                E[e.id].flow -= push_flow;
                            }

                            // add node e.to to active
                            if(pre_balance <= 0 and balance[e.to] > 0){
                                active.push(e.to);
                            }                  

                            if(balance[v] == 0){
                                active.pop();
                                break;
                            }
                        }

                        current_arc[v]++;
                    }
                }
            }

            // print_E();
            // dump(balance)
            // dump(dual)
            // print_residual_graph();

            if(eps == 2) break;
            eps /= scaling_factor;
        }
        
        return calc_objective();
    }

};

int main(){

    int N, K;
    cin >> N >> K;
    vector<int> A(N), B(N);
    for(int i=0;i<N;i++) cin >> A[i];
    for(int i=0;i<N;i++) cin >> B[i];
    vector<vector<int>> P(N, vector<int>(N, 0));
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            cin >> P[i][j];
        }
    }

    cs_graph<int, ll> G(2*N+2);
    int s = 2*N, t = s+1;

    for(int i=0;i<N;i++){
        G.add_edge(s, i, A[i], 0, 0);
    }

    ll S = 0;

    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            S += P[i][j] * P[i][j];
            for(int k=0;k<A[i];k++){
                G.add_edge(i, N+j, 1, 0, 2*k+1-2*P[i][j]);
            }
        }
    }

    for(int i=0;i<N;i++){
        G.add_edge(N+i, t, B[i], 0, 0);
    }

    vector<int> b(2*N+2, 0);
    b[s] = K;
    b[t] = -K; 

    G.set_b(b);
    cout << G.cost_scaling() + S << endl;

    return 0;
}
0