forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI.cpp
More file actions
186 lines (165 loc) · 5.27 KB
/
I.cpp
File metadata and controls
186 lines (165 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define inf 1000000005
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))
#define mod 1000000007
#define sz(a) ((int)(a).size())
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcount(s);}
#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define REP(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
#define eps 1e-12
typedef pair<int, int> II;
typedef pair<ll, ll> LL;
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define PI (2 * acos(0))
#define linf (1ll << 60)
#define maxn 10005
#define maxe 1000005
#define maxv 50005
template<class Flow=int, class Cost=int>
struct MinCostFlow {
const Flow INF_FLOW = 1000111000;
const Cost INF_COST = 1000111000;
int n, t, S, T;
Flow totalFlow;
Cost totalCost;
vector<int> last, visited;
vector<Cost> dis;
struct Edge {
int to;
Flow cap;
Cost cost;
int next;
Edge(int to, Flow cap, Cost cost, int next) :
to(to), cap(cap), cost(cost), next(next) {}
};
vector<Edge> edges;
MinCostFlow(int n) : n(n), t(0), totalFlow(0), totalCost(0), last(n, -1), visited(n, 0), dis(n, 0) {
edges.clear();
}
int add(int from, int to, Flow cap, Cost cost) {
edges.push_back(Edge(to, cap, cost, last[from]));
last[from] = t++;
edges.push_back(Edge(from, 0, -cost, last[to]));
last[to] = t++;
return t - 2;
}
pair<Flow, Cost> minCostFlow(int _S, int _T) {
S = _S; T = _T;
SPFA();
while (1) {
while (1) {
REP(i,n) visited[i] = 0;
if (!findFlow(S, INF_FLOW)) break;
}
if (!modifyLabel()) break;
}
return make_pair(totalFlow, totalCost);
}
private:
void SPFA() {
REP(i,n) dis[i] = INF_COST;
priority_queue< pair<Cost,int> > Q;
Q.push(make_pair(dis[S]=0, S));
while (!Q.empty()) {
int x = Q.top().second;
Cost d = -Q.top().first;
Q.pop();
// For double: dis[x] > d + EPS
if (dis[x] != d) continue;
for(int it = last[x]; it >= 0; it = edges[it].next)
if (edges[it].cap > 0 && dis[edges[it].to] > d + edges[it].cost)
Q.push(make_pair(-(dis[edges[it].to] = d + edges[it].cost), edges[it].to));
}
Cost disT = dis[T]; REP(i,n) dis[i] = disT - dis[i];
}
Flow findFlow(int x, Flow flow) {
if (x == T) {
totalCost += dis[S] * flow;
totalFlow += flow;
return flow;
}
visited[x] = 1;
Flow now = flow;
for(int it = last[x]; it >= 0; it = edges[it].next)
// For double: fabs(dis[edges[it].to] + edges[it].cost - dis[x]) < EPS
if (edges[it].cap && !visited[edges[it].to] && dis[edges[it].to] + edges[it].cost == dis[x]) {
Flow tmp = findFlow(edges[it].to, min(now, edges[it].cap));
edges[it].cap -= tmp;
edges[it ^ 1].cap += tmp;
now -= tmp;
if (!now) break;
}
return flow - now;
}
bool modifyLabel() {
Cost d = INF_COST;
REP(i,n) if (visited[i])
for(int it = last[i]; it >= 0; it = edges[it].next)
if (edges[it].cap && !visited[edges[it].to])
d = min(d, dis[edges[it].to] + edges[it].cost - dis[i]);
// For double: if (d > INF_COST / 10) INF_COST = 1e20
if (d == INF_COST) return false;
REP(i,n) if (visited[i])
dis[i] += d;
return true;
}
};
int n, k;
int a[maxn][4];
int main(){
// freopen("in.txt", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
For(i, 1, k){
For(j, 1, n){
int x;
cin >> x;
a[x][i] = j;
}
}
MinCostFlow<int,int> mcf(n * 2 + 2);
For(i, 1, n){
mcf.add(0, i, 1, 0);
mcf.add(n + i, n + n + 1, 1, 0);
}
For(i, 1, n){
vector<int> V;
For(j, 1, k) For(t, -8, 8){
int x = a[i][j] + t;
if(x > 0 && x <= n){
V.pb(x);
}
}
sort(all(V));
V.resize(unique(all(V)) - V.begin());
Rep(j, sz(V)){
int x = V[j];
int ret = 0;
For(t, 1, k){
ret += min(abs(x - a[i][t]), 8);
}
// cout << i << " " << x << endl;
mcf.add(i, x + n, 1, ret);
}
}
auto t = mcf.minCostFlow(0, n*2+1);
cout << t.second << endl;
return 0;
}