# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
255369 |
2020-07-31T20:08:19 Z |
VEGAnn |
Domino (COCI15_domino) |
C++14 |
|
532 ms |
211192 KB |
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("no-stack-protector")
#define PB push_back
#define i2 array<int,2>
#define sz(x) ((int)x.size())
using namespace std;
typedef long long ll;
const int N = 2010;
const int oo = 2e9;
const int cnst = 50;
vector<i2> who[N];
ll glob = 0;
int n, k, a[N][N], S, T, GO[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int dst[N * N], pre[N * N];
bool good[N][N];
struct edge{
int a, b, cap, flow, cost;
edge(): a(0), b(0), cap(0), flow(0), cost(0) {}
edge(int _a, int _b, int c, int f, int co): a(_a), b(_b), cap(c), flow(f), cost(co) {}
};
vector<edge> edges;
vector<int> g[N * N];
void add_edge(int a, int b, int cst){
g[a].PB(sz(edges));
edges.PB(edge(a, b, 1, 0, cst));
g[b].PB(sz(edges));
edges.PB(edge(b, a, 0, 0, -cst));
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n >> k;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
cin >> a[i][j];
glob += a[i][j];
}
S = n * n;
T = n * n + 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++){
if (i > 0)
who[a[i][j] + a[i - 1][j]].PB({i * n + j, (i - 1) * n + j});
if (j > 0)
who[a[i][j] + a[i][j - 1]].PB({i * n + j, i * n + j - 1});
}
int rem = cnst;
for (int i = N - 1; i >= 0 && rem > 0; i--)
for (int itr = 0; itr < sz(who[i]) && rem > 0; itr++){
i2 cur = who[i][itr];
good[cur[0] / n][cur[0] % n] = 1;
good[cur[1] / n][cur[1] % n] = 1;
rem--;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if ((i + j) & 1){
if (good[i][j])
add_edge(i * n + j, T, 0);
} else {
if (!good[i][j]) continue;
add_edge(S, i * n + j, 0);
for (int it = 0; it < 4; it++){
int x = i + GO[it][0];
int y = j + GO[it][1];
if (x < 0 || x >= n || y < 0 || y >= n || !good[x][y]) continue;
add_edge(i * n + j, x * n + y, -(a[i][j] + a[x][y]));
}
}
ll cost = 0;
for (int itr = 0; itr < k; itr++){
fill(dst, dst + T + 1, oo);
dst[S] = 0;
for (int nit = 0; nit < 2 * cnst; nit++){
for (int idx = 0; idx < sz(edges); idx++) {
edge cr = edges[idx];
if (dst[cr.a] < oo && cr.flow < cr.cap && dst[cr.b] > dst[cr.a] + cr.cost) {
dst[cr.b] = dst[cr.a] + cr.cost;
pre[cr.b] = idx;
}
}
}
cost += dst[T];
int pos = T;
while (pos != S){
int id = pre[pos];
edges[id].flow++;
edges[id ^ 1].flow--;
pos = edges[id].a;
}
}
cout << glob + cost;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
106488 KB |
Output is correct |
2 |
Correct |
86 ms |
104168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
95740 KB |
Output is correct |
2 |
Correct |
57 ms |
95628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
483 ms |
211192 KB |
Output is correct |
2 |
Correct |
495 ms |
190524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
95352 KB |
Output is correct |
2 |
Correct |
67 ms |
95352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
332 ms |
166252 KB |
Output is correct |
2 |
Correct |
312 ms |
173708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
181 ms |
130428 KB |
Output is correct |
2 |
Correct |
180 ms |
123732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
532 ms |
210196 KB |
Output is correct |
2 |
Correct |
487 ms |
189992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
95736 KB |
Output is correct |
2 |
Correct |
60 ms |
95608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
527 ms |
209656 KB |
Output is correct |
2 |
Correct |
505 ms |
190608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
96248 KB |
Output is correct |
2 |
Correct |
58 ms |
96124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
95352 KB |
Output is correct |
2 |
Correct |
65 ms |
95448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
500 ms |
209564 KB |
Output is correct |
2 |
Correct |
500 ms |
189684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
95868 KB |
Output is correct |
2 |
Correct |
59 ms |
95864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
180 ms |
130040 KB |
Output is correct |
2 |
Correct |
164 ms |
123088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
95352 KB |
Output is correct |
2 |
Correct |
57 ms |
95256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
518 ms |
208800 KB |
Output is correct |
2 |
Correct |
497 ms |
189656 KB |
Output is correct |