結果

問題 No.1324 Approximate the Matrix
ユーザー carrot46carrot46
提出日時 2020-12-23 13:16:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 127 ms / 2,000 ms
コード長 4,953 bytes
コンパイル時間 2,195 ms
コンパイル使用メモリ 192,620 KB
実行使用メモリ 6,764 KB
最終ジャッジ日時 2023-10-21 15:08:42
合計ジャッジ時間 4,928 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 107 ms
6,764 KB
testcase_04 AC 107 ms
6,764 KB
testcase_05 AC 108 ms
6,764 KB
testcase_06 AC 107 ms
6,632 KB
testcase_07 AC 107 ms
6,632 KB
testcase_08 AC 13 ms
5,640 KB
testcase_09 AC 7 ms
4,348 KB
testcase_10 AC 16 ms
4,676 KB
testcase_11 AC 32 ms
5,660 KB
testcase_12 AC 5 ms
4,348 KB
testcase_13 AC 4 ms
4,348 KB
testcase_14 AC 39 ms
6,000 KB
testcase_15 AC 15 ms
5,532 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 16 ms
4,348 KB
testcase_18 AC 6 ms
4,348 KB
testcase_19 AC 4 ms
4,348 KB
testcase_20 AC 5 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 6 ms
5,312 KB
testcase_23 AC 10 ms
4,348 KB
testcase_24 AC 41 ms
5,576 KB
testcase_25 AC 18 ms
4,348 KB
testcase_26 AC 19 ms
4,504 KB
testcase_27 AC 7 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 125 ms
6,764 KB
testcase_38 AC 126 ms
6,764 KB
testcase_39 AC 126 ms
6,764 KB
testcase_40 AC 127 ms
6,764 KB
testcase_41 AC 126 ms
6,764 KB
testcase_42 AC 9 ms
6,632 KB
testcase_43 AC 8 ms
6,632 KB
testcase_44 AC 9 ms
6,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <chrono>
#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T &b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T &b){
    if(a < b) {a = b; return true;}
    else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
    if(!v.empty()){
        for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
        std::cout<<v.back();
    }
    if(endline) std::cout<<std::endl;
}

struct edge{
    int to, cap, rev;
    ll cost;
    edge(){}
    edge(int a, int b, ll c, int d){
        to = a;
        cap = b;
        cost = c;
        rev = d;
    }
};
using ve = vector<edge>;

void add_edge(int from, int to, int cap, ll cost, vector<ve> &G, bool directed = true){
    rep(_, (directed ? 1 : 2)) {
        G[from].emplace_back(to, cap, cost, (int) G[to].size());
        G[to].emplace_back(from, 0, -cost, (int) G[from].size() - 1);
        swap(to, from);
    }
}

bool bellman_ford_for_flow(int s, vector<ve> &G, vec &dist){
    //O(|V|^2 + |V||E|)
    dist[s] = 0;
    int num(0), sz = G.size();
    while(num++ < sz){
        bool update = false;
        rep(v, sz){
            if(dist[v] != INF) {
                for (edge &e : G[v]) {
                    if (e.cap > 0 && chmin(dist[e.to], dist[v] + e.cost)) update = true;
                }
            }
        }
        if(!update) return true;
    }

    return false; //failed
}

mat cost_v, cost_v_first;
void dijkstra_for_flow(int s, vector<ve> &G, vec &h, vec &dist, vector<int> &prev_v, vector<int> &prev_e){
    typedef struct _comp{
        ll cost;
        int v;
        _comp(){}
        _comp(ll a, int b){
            cost = a;
            v = b;
        }
        bool operator > (const _comp a){
            return cost > a.cost;
        }
    } mp;
    priority_queue<mp, vector<mp>, greater<> > pque;
    dist[s] = 0;
    pque.emplace(0, s);
    while(!pque.empty()){
        mp p = pque.top(); pque.pop();
        if(dist[p.v] < p.cost) continue;
        rep(i, (int)G[p.v].size()){
            edge &e = G[p.v][i];
            if(p.v < N * 2 && e.to < N * 2){
                e.cost = (p.v < e.to ? cost_v.at(p.v).at(e.to - N) : -cost_v.at(e.to).at(p.v - N) + 2);
            }
            if(e.cap > 0 && chmin(dist[e.to], p.cost + e.cost + h[p.v] - h[e.to])){
                prev_v[e.to] = p.v;
                prev_e[e.to] = i;
                pque.emplace(dist[e.to], e.to);
            }
        }
    }
}

//全然Validateできていないので注意
ll min_cost_flow(int s, int t, int f, vector<ve> &G, bool minus_exist = true){
    //minus_exist : if (edge.cost < 0)
    //for graph without negative cycles
    int sz = G.size();
    ll res(0);
    vector<int> prev_v(sz), prev_e(sz);
    vec h(sz, minus_exist ? INF : 0);
    if(minus_exist && (!bellman_ford_for_flow(s, G, h) || h[t] == INF)) return -1;
    while(f > 0){
        vec dist(sz, INF);
        dijkstra_for_flow(s, G, h, dist, prev_v, prev_e);
        if(dist[t] == INF) return -1;
        rep(v, sz) h[v] += dist[v];
        int d = f;
        for(int v = t; v != s; v = prev_v[v]){
            chmin(d, G[prev_v[v]][prev_e[v]].cap);
        }
        f -= d;
        res += h[t] * d;
        for(int v = t; v != s; v = prev_v[v]){
            edge &e = G[prev_v[v]][prev_e[v]];
            if(v < N * 2 && prev_v[v] < N * 2){
                int u = prev_v[v];
                if(u < v) {
                    cost_v[u][v - N] += 2;
                    G[v][e.rev].cap = 1;
                }else {
                    cost_v[v][u - N] -= 2;
                    if (cost_v[v][u - N] == cost_v_first[v][u - N]){
                        e.cap = 0;
                    }
                }
            }else {
                e.cap -= d;
                G[v][e.rev].cap += d;
            }
        }
    }
    return res;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin>>N>>K;
    const int st = N * 2, gl = N * 2 + 1;
    vector<ve> G(N * 2 + 2);
    rep(i, N){
        cin>>A;
        add_edge(st, i, A, 0, G);
    }
    rep(i, N){
        cin>>A;
        add_edge(i+N, gl, A, 0, G);
    }
    ll sum = - (K * 400);
    cost_v.resize(N);
    rep(i, N){
        cost_v[i].resize(N);
        reps(j, N, N*2){
            cin>>A;
            sum += A * A;
            cost_v[i][j-N] = 400 - A * 2 + 1;
            add_edge(i, j, 1, 0, G);
        }
    }
    cost_v_first = cost_v;
    cout<<sum + min_cost_flow(st, gl, (int)K, G, false)<<endl;
}
0