結果

問題 No.1324 Approximate the Matrix
ユーザー chocoruskchocorusk
提出日時 2020-12-21 00:27:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 124 ms / 2,000 ms
コード長 2,918 bytes
コンパイル時間 1,495 ms
コンパイル使用メモリ 137,052 KB
実行使用メモリ 8,636 KB
最終ジャッジ日時 2023-10-21 11:21:53
合計ジャッジ時間 4,196 ms
ジャッジサーバーID
(参考情報)
judge12 / 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 93 ms
7,748 KB
testcase_04 AC 91 ms
7,632 KB
testcase_05 AC 94 ms
7,628 KB
testcase_06 AC 93 ms
7,708 KB
testcase_07 AC 93 ms
7,748 KB
testcase_08 AC 14 ms
5,596 KB
testcase_09 AC 7 ms
4,348 KB
testcase_10 AC 15 ms
4,920 KB
testcase_11 AC 30 ms
5,632 KB
testcase_12 AC 6 ms
4,348 KB
testcase_13 AC 5 ms
4,348 KB
testcase_14 AC 34 ms
6,048 KB
testcase_15 AC 15 ms
5,488 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 17 ms
4,732 KB
testcase_18 AC 7 ms
4,348 KB
testcase_19 AC 5 ms
4,348 KB
testcase_20 AC 5 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 9 ms
5,424 KB
testcase_23 AC 14 ms
4,348 KB
testcase_24 AC 39 ms
5,912 KB
testcase_25 AC 19 ms
4,592 KB
testcase_26 AC 19 ms
4,676 KB
testcase_27 AC 9 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 122 ms
8,588 KB
testcase_38 AC 124 ms
8,612 KB
testcase_39 AC 124 ms
8,632 KB
testcase_40 AC 122 ms
8,616 KB
testcase_41 AC 124 ms
8,636 KB
testcase_42 AC 15 ms
6,164 KB
testcase_43 AC 14 ms
6,160 KB
testcase_44 AC 15 ms
6,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <stack>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

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

int V;
vector<edge> g[1010];
ll h[1010];
ll dist[1010];
int prevv[1010], preve[1010];

void add_edge(int from, int to, int cap, ll cost){
    edge e=edge(to, cap, cost, g[to].size());
    g[from].push_back(e);
    e=edge(from, 0, -cost, g[from].size()-1);
    g[to].push_back(e);
}

ll min_cost_flow(int s, int t, int f){
    using P=pair<ll, int>;
    const ll INF=1e18;
    ll res=0;
    fill(h, h+V, 0);
    while(f>0){
        priority_queue<P, vector<P>, greater<P>> que;
        fill(dist, dist+V, INF);
        dist[s]=0;
        que.push({0, s});
        while(!que.empty()){
            P p=que.top(); que.pop();
            int v=p.second;
            if(dist[v]<p.first) 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+h[v]-h[e.to]){
                    dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
                    prevv[e.to]=v;
                    preve[e.to]=i;
                    que.push({dist[e.to], e.to});
                }
            }
        }
        for(int v=0; v<V; v++) h[v]+=dist[v];
        if(dist[t]==INF) return -1;
        int d=f;
        for(int v=t; v!=s; v=prevv[v]){
            d=min(d, g[prevv[v]][preve[v]].cap);
        }
        f-=d;
        res+=d*h[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;
}
int main()
{
	int n, k;
    cin>>n>>k;
    int a[202], b[202];
    for(int i=0; i<n; i++){
        cin>>a[i];
    }
    for(int i=0; i<n; i++){
        cin>>b[i];
    }
    int p[202][202];
    ll ans=0;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            cin>>p[i][j];
            ans+=p[i][j]*p[i][j];
        }
    }
    V=2*n+2;
    int s=2*n, t=2*n+1;
    for(int i=0; i<n; i++){
        add_edge(s, i, a[i], 0);
    }
    for(int i=0; i<n; i++){
        add_edge(i+n, t, b[i], 0);
    }
    ll mx=400;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            for(int l=0; l<=min(a[i], b[j]); l++){
                add_edge(i, j+n, 1, -2*p[i][j]+1+2*l+mx);
            }
        }
    }
    ans+=min_cost_flow(s, t, k);
    ans-=k*mx;
    cout<<ans<<endl;
    return 0;
}
0