結果
| 問題 |
No.1324 Approximate the Matrix
|
| コンテスト | |
| ユーザー |
kawara_y_kyopro
|
| 提出日時 | 2020-12-24 04:16:11 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 77 ms / 2,000 ms |
| コード長 | 4,997 bytes |
| コンパイル時間 | 6,358 ms |
| コンパイル使用メモリ | 171,980 KB |
| 最終ジャッジ日時 | 2025-01-17 06:37:51 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 42 |
ソースコード
#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;
}
kawara_y_kyopro