#include <bits/stdc++.h>
using namespace std;
#define FNAME "test"
template<typename T> inline bool minimize(T& _a, T _b) { return _a > _b ? _a = _b, true : false; };
template<typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
typedef long long ll;
struct MCMF {
using F = ll;
using C = ll;
struct Edge {
int to;
F flow, cap;
C cost;
Edge() {}
Edge(int to, F flow, F cap, C cost) : to(to), flow(flow), cap(cap), cost(cost) {}
};
int N, M;
vector<C> p, dist;
vector<int> pre;
vector<Edge> edges;
vector<vector<int>> adj;
void init(int _N) {
N = _N; M = 0;
p.resize(N);
dist.resize(N);
pre.resize(N);
adj.resize(N);
}
void add_edge(int u, int v, F cap, C cost) {
assert(cap >= 0);
adj[u].push_back(M++); edges.push_back(Edge(v, 0, cap, cost));
adj[v].push_back(M++); edges.push_back(Edge(u, 0, 0, -cost));
}
bool find_min_cost_path(int s, int t) {
const C oo = numeric_limits<C>::max();
for (int i = 0; i < N; i++) dist[i] = oo;
using T = pair<C,int>; min_heap<T> pq;
pq.push(make_pair(dist[s] = 0, s));
while (!pq.empty()) {
T x = pq.top(); pq.pop();
if (x.first > dist[x.second]) continue;
for (int e_id : adj[x.second]) {
const Edge& E = edges[e_id];
if (E.flow < E.cap && minimize(dist[E.to], x.first + E.cost + p[x.second] - p[E.to])) {
pre[E.to] = e_id;
pq.push(make_pair(dist[E.to], E.to));
}
}
}
return dist[t] != oo;
}
pair<F,C> calc(int s, int t) {
assert(s != t);
for (int _ = 0; _ < N; _++) {
for (int e_id = 0; e_id < M; e_id++) {
const Edge& E = edges[e_id];
if (E.cap) minimize(p[E.to], p[edges[e_id ^ 1].to] + E.cost);
}
}
F totalFlow = 0;
C totalCost = 0;
while (find_min_cost_path(s, t)) {
for (int i = 0; i < N; i++) p[i] += dist[i];
F df = numeric_limits<F>::max();
for (int x = t; x != s; x = edges[pre[x] ^ 1].to) {
const Edge& E = edges[pre[x]];
minimize(df, E.cap - E.flow);
}
totalFlow += df;
totalCost += df * (p[t] - p[s]);
for (int x = t; x != s; x = edges[pre[x] ^ 1].to) {
edges[pre[x]].flow += df;
edges[pre[x] ^ 1].flow -= df;
}
}
return make_pair(totalFlow, totalCost);
}
};
int n, k;
ll sum, ans;
MCMF MinCostMaxFlow;
void Task() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(9);
if (fopen(FNAME".inp","r")) {
freopen(FNAME".inp","r",stdin);
freopen(FNAME".out","w",stdout);
}
}
void Solve() {
//Your Code
cin >> n >> k;
MinCostMaxFlow.init(n * n + 3);
MinCostMaxFlow.add_edge(n * n + 2, 0, k, 0);
sum = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
ll x;
cin >> x;
sum += x;
int id = (i - 1) * n + j;
if ((i + j) % 2) {
MinCostMaxFlow.add_edge(0, id, 1, -x);
if (j != n) MinCostMaxFlow.add_edge(id, id + 1, 1, 0);
if (i != n) MinCostMaxFlow.add_edge(id, id + n, 1, 0);
if (j != 1) MinCostMaxFlow.add_edge(id, id - 1, 1, 0);
if (i != 1) MinCostMaxFlow.add_edge(id, id - n, 1, 0);
} else {
MinCostMaxFlow.add_edge(id, n * n + 1, 1, -x);
}
}
}
cout << sum + MinCostMaxFlow.calc(n * n + 2, n * n + 1).second << '\n';
}
int main() {
Task();
Solve();
cerr << "\nTime run: " << 1000*clock()/CLOCKS_PER_SEC << "ms";
return 37^37;
}
컴파일 시 표준 에러 (stderr) 메시지
domino.cpp: In function 'void Task()':
domino.cpp:102:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | freopen(FNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
domino.cpp:103:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | freopen(FNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |