結果

問題 No.1324 Approximate the Matrix
ユーザー kawara_y_kyoprokawara_y_kyopro
提出日時 2020-12-24 04:16:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 4,997 bytes
コンパイル時間 2,403 ms
コンパイル使用メモリ 142,056 KB
実行使用メモリ 6,056 KB
最終ジャッジ日時 2023-10-21 15:33:14
合計ジャッジ時間 4,764 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 67 ms
6,052 KB
testcase_04 AC 66 ms
6,052 KB
testcase_05 AC 68 ms
6,052 KB
testcase_06 AC 70 ms
6,052 KB
testcase_07 AC 69 ms
6,052 KB
testcase_08 AC 7 ms
4,348 KB
testcase_09 AC 5 ms
4,348 KB
testcase_10 AC 10 ms
4,348 KB
testcase_11 AC 20 ms
4,612 KB
testcase_12 AC 6 ms
4,348 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 21 ms
4,656 KB
testcase_15 AC 8 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 13 ms
4,348 KB
testcase_18 AC 4 ms
4,348 KB
testcase_19 AC 5 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 3 ms
4,348 KB
testcase_23 AC 11 ms
4,348 KB
testcase_24 AC 27 ms
4,996 KB
testcase_25 AC 15 ms
4,348 KB
testcase_26 AC 14 ms
4,348 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 1 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 71 ms
6,056 KB
testcase_38 AC 77 ms
5,788 KB
testcase_39 AC 71 ms
5,788 KB
testcase_40 AC 73 ms
5,788 KB
testcase_41 AC 74 ms
6,048 KB
testcase_42 AC 5 ms
4,348 KB
testcase_43 AC 5 ms
4,348 KB
testcase_44 AC 4 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
// #include <atcoder/all>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vii = V<int>;
using vvll = V<vll>;
using vvii = V< V<int> >;
using PII = P<int, int>;
using PLL = P<ll, ll>;
#define RevREP(i,n,a) for(ll i=n;i>a;i--)  // (a,n]
#define REP(i,a,n) for(ll i=a;i<n;i++)  // [a,n)
#define rep(i, n) REP(i,0,n)
#define ALL(v) v.begin(),v.end()
#define eb emplace_back
#define pb push_back
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }

template<class A, class B>
ostream& operator <<(ostream& out, const P<A, B> &p) {
    return out << '(' << p.first << ", " << p.second << ')';
}
template<class A>
ostream& operator <<(ostream& out, const V<A> &v) {
    out << '[';
    for (int i=0;i<int(v.size());i++) {
        if (i) out << ", ";
        out << v[i];
    }
    return out << ']';
}

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

template <typename Cap, typename Cost>
class MinimumCostFlow {
    typedef std::pair<Cost, int> P;

    struct edge {
        int to, rev;
        Cap cap;
        Cost cost;
    };
public:
    int V;
    std::vector< std::vector<edge> > G;
    std::vector<Cost> h, dist;  // ポテンシャル、最短距離
    std::vector<int> prevv, preve;  // 直前の頂点と辺

    MinimumCostFlow(int n): V(n), G(n), h(n), dist(n), prevv(n), preve(n) {}

    // from から to へ向かう容量 cap、コスト cost の辺をグラフに追加する
    void add_edge(int from, int to, Cap cap, Cost cost) {
        G[from].push_back({to, int(G[to].size()), cap, cost});
        G[to].push_back({from, int(G[from].size()) - 1, 0, -cost});
    }

    // s から t への流量 f の最小費用流を求める
    // 流せない場合は -1
    Cost min_cost_flow(int s, int t, Cap f) {
        Cost res = 0;
        std::fill(h.begin(), h.end(), 0);
        while (f > 0) {
            // Dijkstra 法による h の更新
            std::priority_queue< P, std::vector< P >, std::greater< P > > que;
            std::fill(dist.begin(), dist.end(), std::numeric_limits<Cost>::max());
            dist[s] = 0;
            que.push(P(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 < (int)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(P(dist[e.to], e.to));
                    }
                }
            }

            // これ以上流せない時
            if (dist[t] == std::numeric_limits<Cost>::max()) {
                return -1;
            }
            
            for (int v = 0; v < V; v++) h[v] += dist[v];

            // s-t 間最短路に沿って目一杯流す
            Cap d = f;
            for (int v = t; v != s; v = prevv[v]) {
                d = std::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;
    }
};

auto p2 = [](ll p) {return p * p;};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, k; cin >> n >> k;
    vii a(n), b(n);
    vvii p(n, vii(n));
    ll ans = 0;
    rep(i, n) cin >> a[i];
    rep(i, n) cin >> b[i];
    rep(i, n) rep(j, n) {
        cin >> p[i][j];
        ans += p2(p[i][j]);
    }
    int s = 2 * n, t = 2 * n + 1;
    MinimumCostFlow<ll, ll> mc(2 * n + 2);
    rep(i, n) {
        mc.add_edge(s, i, a[i], 0);
        mc.add_edge(i + n, t, b[i], 0);
        rep(j, n) {
            REP(ai, 1, a[i] + 1) mc.add_edge(i, j + n, 1, p2(ai - p[i][j]) - p2(ai - 1 - p[i][j]) + INF);
        }
    }

    ll cost = mc.min_cost_flow(s, t, k);
    cout << ans - ll(k) * INF + cost << '\n';
    return 0;
}
0