結果
| 問題 |
No.1324 Approximate the Matrix
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-12-21 00:07:44 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 121 ms / 2,000 ms |
| コード長 | 3,738 bytes |
| コンパイル時間 | 1,426 ms |
| コンパイル使用メモリ | 114,336 KB |
| 実行使用メモリ | 22,388 KB |
| 最終ジャッジ日時 | 2024-09-21 12:32:52 |
| 合計ジャッジ時間 | 5,445 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 42 |
ソースコード
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
namespace MCF {
#define MAXN 1'000'000
#define MAXM 1'000'000
#define wint int
#define cint int
const wint wEPS = 0;
const wint wINF = 1001001001;
const cint cEPS = 0;
const cint cINF = 1001001001;
int n, m, ptr[MAXN], next[MAXM], zu[MAXM];
wint capa[MAXM], tof;
cint cost[MAXM], toc, d[MAXN], pot[MAXN];
int vis[MAXN], pree[MAXN];
void init(int _n) {
n = _n; m = 0; memset(ptr, ~0, n << 2);
}
void ae(int u, int v, wint w, cint c) {
next[m] = ptr[u]; ptr[u] = m; zu[m] = v; capa[m] = w; cost[m] = +c; ++m;
next[m] = ptr[v]; ptr[v] = m; zu[m] = u; capa[m] = 0; cost[m] = -c; ++m;
}
bool solve(int src, int ink, wint flo = wINF) {
wint f;
cint c, cc;
int i, u, v;
memset(pot, 0, n * sizeof(cint));
//*
for (bool cont = 1; cont; ) {
cont = 0;
for (u = 0; u < n; ++u) for (i = ptr[u]; ~i; i = next[i]) if (capa[i] > wEPS) {
if (pot[zu[i]] > pot[u] + cost[i] + cEPS) {
pot[zu[i]] = pot[u] + cost[i]; cont = 1;
}
}
}
//*/
for (toc = 0, tof = 0; tof + wEPS < flo; ) {
typedef pair<cint,int> node;
priority_queue< node,vector<node>,greater<node> > q;
for (u = 0; u < n; ++u) { d[u] = cINF; vis[u] = 0; }
for (q.push(make_pair(d[src] = 0, src)); !q.empty(); ) {
c = q.top().first; u = q.top().second; q.pop();
if (vis[u]++) continue;
for (i = ptr[u]; ~i; i = next[i]) if (capa[i] > wEPS) {
cc = c + cost[i] + pot[u] - pot[v = zu[i]];
if (d[v] > cc) { q.push(make_pair(d[v] = cc, v)); pree[v] = i; }
}
}
if (!vis[ink]) return 0;
f = flo - tof;
for (v = ink; v != src; v = zu[i ^ 1]) { i = pree[v]; chmin(f, capa[i]); }
for (v = ink; v != src; v = zu[i ^ 1]) { i = pree[v]; capa[i] -= f; capa[i ^ 1] += f; }
tof += f;
toc += f * (d[ink] - pot[src] + pot[ink]);
for (u = 0; u < n; ++u) pot[u] += d[u];
}
return 1;
}
}
int sq(int x) {
return x * x;
}
int N, K;
vector<int> A, B;
vector<vector<int>> P;
int main() {
for (; ~scanf("%d%d", &N, &K); ) {
A.resize(N);
for (int x = 0; x < N; ++x) {
scanf("%d", &A[x]);
}
B.resize(N);
for (int y = 0; y < N; ++y) {
scanf("%d", &B[y]);
}
P.assign(N, vector<int>(N));
for (int x = 0; x < N; ++x) for (int y = 0; y < N; ++y) {
scanf("%d", &P[x][y]);
}
MCF::init(2 + N + N);
int ans = 0;
for (int x = 0; x < N; ++x) {
MCF::ae(0, 2 + x, A[x], 0);
}
for (int y = 0; y < N; ++y) {
MCF::ae(2 + N + y, 1, B[y], 0);
}
for (int x = 0; x < N; ++x) for (int y = 0; y < N; ++y) {
ans += sq(0 - P[x][y]);
const int lim = min(A[x], B[y]);
for (int i = 0; i < lim; ++i) {
MCF::ae(2 + x, 2 + N + y, 1, sq((i + 1) - P[x][y]) - sq(i - P[x][y]));
}
}
MCF::solve(0, 1, K);
ans += MCF::toc;
printf("%d\n", ans);
}
return 0;
}