# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
258941 |
2020-08-06T19:53:29 Z |
Vimmer |
Domino (COCI15_domino) |
C++14 |
|
3949 ms |
275816 KB |
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#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 F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define pf push_front
#define N 100050
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9
using namespace std;
//using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef short int si;
typedef array <ll, 5> a5;
typedef array <ll, 4> a4;
//typedef tree <ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
unordered_map <int, unordered_map <int, int> > mp;
si a[2001][2001];
char mk[2001][2001];
int id = 2;
ll sum;
int ans, dp[500], pr[500];
vector <a5> edges;
bool cmp(a4 x, a4 y) {return a[x[0]][x[1]] + a[x[2]][x[3]] > a[y[0]][y[1]] + a[y[2]][y[3]];}
vector <a4 > gr;
vector <pair <int, int> > wh, bl;
bool opr(int x, int y) {return ((x + y) % 2) == 0;}
void add(pair <int, int> t) {if (mk[t.F][t.S] == 'b') return; mk[t.F][t.S] = 'b'; if (opr(t.F, t.S)) bl.pb({t.F, t.S}); else wh.pb({t.F, t.S});}
void add_edge(int from, int to, int cost)
{
edges.pb({from, to, cost, 0, 1});
edges.pb({to, from, -cost, 0, 0});
}
int main()
{
//freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, k;
cin >> n >> k;
for (register int i = 0; i < n; i++)
for (register int j = 0; j < n; j++)
{
cin >> a[i][j];
sum += ll(a[i][j]);
if (i != 0) gr.pb({i, j, i - 1, j});
if (j != 0) gr.pb({i, j, i, j - 1});
}
sort(gr.begin(), gr.end(), cmp);
for (int i = 0; i < min(50, sz(gr)); i++)
{
add({gr[i][0], gr[i][1]}); add({gr[i][2], gr[i][3]});
pair <int, int> pt; pt.F = gr[i][0]; pt.S = gr[i][1];
if (pt.F != 0) add({pt.F - 1, pt.S});
if (pt.S != 0) add({pt.F, pt.S - 1});
if (pt.F + 1 != n) add({pt.F + 1, pt.S});
if (pt.S + 1 != n) add({pt.F, pt.S + 1});
pt.F = gr[i][2]; pt.S = gr[i][3];
if (pt.F != 0) add({pt.F - 1, pt.S});
if (pt.S != 0) add({pt.F, pt.S - 1});
if (pt.F + 1 != n) add({pt.F + 1, pt.S});
if (pt.S + 1 != n) add({pt.F, pt.S + 1});
}
for (auto it : wh)
{
mp[it.F][it.S] = id++;
add_edge(0, mp[it.F][it.S], 0);
}
for (auto it : bl)
{
mp[it.F][it.S] = id++;
add_edge(mp[it.F][it.S], 1, 0);
}
for (auto it : bl)
for (auto itr : wh)
if (abs(itr.F - it.F) + abs(itr.S - it.S) == 1)
add_edge(mp[itr.F][itr.S], mp[it.F][it.S], -(a[itr.F][itr.S] + a[it.F][it.S]));
for (; k > 0; k--)
{
for (int i = 0; i < 500; i++) dp[i] = 2e9;
dp[0] = 0;
for (int it = 0; it < 100; it++)
for (int i = 0; i < sz(edges); i++)
{
int to = edges[i][1], from = edges[i][0], cost = edges[i][2], flow = edges[i][3], cap = edges[i][4];
if (flow < cap && dp[from] < 2e9 && dp[from] + cost < dp[to])
{
dp[to] = dp[from] + cost;
pr[to] = i;
}
}
ans += dp[1];
int v = 1;
while (v != 0)
{
int id = pr[v];
edges[id][3]++;
edges[id ^ 1][3]--;
v = edges[id][0];
}
}
cout << sum + ll(ans) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
147 ms |
18496 KB |
Output is correct |
2 |
Correct |
100 ms |
18128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
896 KB |
Output is correct |
2 |
Correct |
2 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3869 ms |
267284 KB |
Output is correct |
2 |
Correct |
1844 ms |
268116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2118 ms |
268628 KB |
Output is correct |
2 |
Correct |
1110 ms |
268756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
661 ms |
68252 KB |
Output is correct |
2 |
Correct |
420 ms |
68252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3949 ms |
267220 KB |
Output is correct |
2 |
Correct |
1820 ms |
273624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
896 KB |
Output is correct |
2 |
Correct |
2 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3858 ms |
267296 KB |
Output is correct |
2 |
Correct |
1815 ms |
275816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1788 KB |
Output is correct |
2 |
Correct |
5 ms |
1788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3900 ms |
267380 KB |
Output is correct |
2 |
Correct |
1839 ms |
275668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1280 KB |
Output is correct |
2 |
Correct |
3 ms |
1280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
670 ms |
68148 KB |
Output is correct |
2 |
Correct |
433 ms |
68252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3793 ms |
267356 KB |
Output is correct |
2 |
Correct |
1955 ms |
275796 KB |
Output is correct |