結果

問題 No.1324 Approximate the Matrix
ユーザー milanis48663220milanis48663220
提出日時 2020-12-21 01:11:50
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 731 ms / 2,000 ms
コード長 3,955 bytes
コンパイル時間 1,898 ms
コンパイル使用メモリ 108,708 KB
実行使用メモリ 11,168 KB
最終ジャッジ日時 2023-10-21 11:25:10
合計ジャッジ時間 8,396 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 350 ms
8,212 KB
testcase_04 AC 354 ms
7,792 KB
testcase_05 AC 366 ms
8,204 KB
testcase_06 AC 416 ms
7,936 KB
testcase_07 AC 357 ms
7,936 KB
testcase_08 AC 13 ms
4,504 KB
testcase_09 AC 12 ms
4,348 KB
testcase_10 AC 28 ms
4,416 KB
testcase_11 AC 70 ms
5,332 KB
testcase_12 AC 14 ms
4,348 KB
testcase_13 AC 6 ms
4,348 KB
testcase_14 AC 80 ms
5,620 KB
testcase_15 AC 19 ms
4,504 KB
testcase_16 AC 4 ms
4,348 KB
testcase_17 AC 56 ms
4,660 KB
testcase_18 AC 11 ms
4,348 KB
testcase_19 AC 19 ms
4,348 KB
testcase_20 AC 5 ms
4,348 KB
testcase_21 AC 5 ms
4,348 KB
testcase_22 AC 5 ms
4,348 KB
testcase_23 AC 68 ms
4,656 KB
testcase_24 AC 122 ms
5,656 KB
testcase_25 AC 68 ms
4,504 KB
testcase_26 AC 49 ms
4,504 KB
testcase_27 AC 27 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 731 ms
11,168 KB
testcase_38 AC 647 ms
11,168 KB
testcase_39 AC 636 ms
11,148 KB
testcase_40 AC 706 ms
11,148 KB
testcase_41 AC 721 ms
11,148 KB
testcase_42 AC 7 ms
5,032 KB
testcase_43 AC 7 ms
5,032 KB
testcase_44 AC 7 ms
5,032 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In member function 'void MinCostFlow::add_edge(int, int, ll, ll)':
main.cpp:39:63: warning: narrowing conversion of '(&((MinCostFlow*)this)->MinCostFlow::G.std::vector<std::vector<edge> >::operator[](((std::vector<std::vector<edge> >::size_type)to)))->std::vector<edge>::size()' from 'std::vector<edge>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
   39 |             G[from].push_back((edge){to, cap, cost, G[to].size()});
      |                                                     ~~~~~~~~~~^~
main.cpp:40:66: warning: narrowing conversion of '((&((MinCostFlow*)this)->MinCostFlow::G.std::vector<std::vector<edge> >::operator[](((std::vector<std::vector<edge> >::size_type)from)))->std::vector<edge>::size() - 1)' from 'std::vector<edge>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
   40 |             G[to].push_back((edge){from, 0, -cost, G[from].size()-1});
      |                                                    ~~~~~~~~~~~~~~^~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

const ll INF = 1e18;

struct edge{
    int to;
    ll cap, cost;
    int rev;
};

class MinCostFlow{
    public:
        int V;  // 頂点数
        vector<vector<edge>> G;
        MinCostFlow(int v){
            V = v;
            G = vector<vector<edge>>(v, vector<edge>());
            dist = vector<ll>(v);
            preve = vector<int>(v);
            prevv = vector<int>(v);
        }
        void add_edge(int from, int to, ll cap, ll cost){
            G[from].push_back((edge){to, cap, cost, G[to].size()});
            G[to].push_back((edge){from, 0, -cost, G[from].size()-1});
        }

        ll min_cost_flow(int s, int t, int f){
            ll res = 0;
            while(f > 0){
                fill(dist.begin(), dist.end(), INF);
                dist[s] = 0;
                bool update = true;
                while(update){
                    update = false;
                    for(int v = 0; v < V; v++){
                        if(dist[v] == INF) continue;
                        for(int i = 0; i < G[v].size(); i++){
                            edge &e = G[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] == INF){
                    return -1;
                }
                ll d = f;
                for(int v = t; v != s; v = prevv[v]){
                    d = min(d, G[prevv[v]][preve[v]].cap);
                }
                f -= d;
                res += d*dist[t];
                for(int v = t; v != s; v = prevv[v]){
                    edge &e = G[prevv[v]][preve[v]];
                    e.cap -= d;
                    G[v][e.rev].cap += d;
                }
            }
            return res;
        }

    private:
        vector<ll> dist;
        vector<int> prevv, preve;
};

int n, k;

int row_idx(int i){
    return 1+i;
}

int col_idx(int i){
    return 1+n+n*n+i;
}

int elm_idx(int i, int j){
    return 1+n+n*i+j;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> n >> k;
    vector<int> a(n), b(n);
    vector<vector<int>> p(n, vector<int>(n));
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++) cin >> b[i];
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> p[i][j];
        }
    }
    int m = 2+2*n+n*n;
    int s = 0, t = m-1;
    MinCostFlow mcf(m);
    for(int i = 0; i < n; i++){
        mcf.add_edge(s, row_idx(i), a[i], 0);
    }
    for(int i = 0; i < n; i++){
        mcf.add_edge(col_idx(i), t, b[i], 0);
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int k_min = min(a[i], b[j]);
            for(int k = 1; k <= k_min; k++){
                mcf.add_edge(row_idx(i), elm_idx(i, j), 1, 2*(k-p[i][j])-1);
                mcf.add_edge(elm_idx(i, j), col_idx(j), 1, 0);
            }
        }
    }
    int ans = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            ans += p[i][j]*p[i][j];
        }
    }
    cout << ans+mcf.min_cost_flow(s, t, k) << endl;
}
0