#pragma GCC optimize ("O3") #pragma GCC target ("avx") #include "bits/stdc++.h" // define macro "/D__MAI" using namespace std; typedef long long int ll; #define debugv(v) printf("L%d %s => ",__LINE__,#v);for(auto e:v){cout< ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout< ostream& operator <<(ostream &o, const pair p) { o << "(" << p.first << ":" << p.second << ")"; return o; } #define TIME chrono::system_clock::now() #define MILLISEC(t) (chrono::duration_cast(t).count()) namespace { std::chrono::system_clock::time_point ttt; void tic() { ttt = TIME; } void toc() { fprintf(stderr, "TIME : %lldms\n", MILLISEC(TIME - ttt)); } std::chrono::system_clock::time_point tle = TIME; #ifdef __MAI void safe_tle(int msec) { assert(MILLISEC(TIME - tle) < msec); } #else #define safe_tle(k) ; #endif } #ifdef __MAI //_getchar_nolock #define getchar_unlocked getchar #endif namespace { #define isvisiablechar(c) (0x21<=(c)&&(c)<=0x7E) class MaiScanner { public: template void input_integer(T& var) { var = 0; T sign = 1; int cc = getchar_unlocked(); for (; cc<'0' || '9'>(int& var) { input_integer(var); return *this; } inline MaiScanner& operator>>(long long& var) { input_integer(var); return *this; } inline MaiScanner& operator>>(string& var) { int cc = getchar_unlocked(); for (; !isvisiablechar(cc); cc = getchar_unlocked()); for (; isvisiablechar(cc); cc = getchar_unlocked()) var.push_back(cc); } }; } MaiScanner scanner; class Flow { public: size_t n; struct Arrow { int from, to; int w_max; int cap; Arrow(int from = 0, int to = 0, int w = 1) :from(from), to(to), w_max(w), cap(w) {} bool operator<(const Arrow& a) const { return (from> vertex_to; vector> vertex_from; vector arrow; Flow(int n, int m = 5010) :n(n), vertex_to(n), vertex_from(n) { arrow.reserve(m); } void connect(int from, int to, int left) { vertex_to[from].push_back(arrow.size()); // toto vertex_from[to].push_back(arrow.size()); // fromfrom arrow.emplace_back(from, to, left); } size_t degree(int v) { return vertex_to[v].size() + vertex_from[v].size(); } size_t degree_in(int v) { return vertex_from[v].size(); } size_t degree_out(int v) { return vertex_to[v].size(); } }; // DAG int _dinic_path_dfs(Flow& flow, vector& result, vector& flag, const vector& dist, int u, int i_sink, int mini) { // TODO: 経路再利用 if (i_sink == u) return mini; if (flag[u]) return -1; flag[u] = true; int sumw = 0; bool term = true; for (int e : flow.vertex_to[u]) { Flow::Arrow& a = flow.arrow[e]; if (a.w_max > 0 && dist[u]>dist[a.to]) { int w; if (mini < 0) w = a.w_max; else w = min(a.w_max, mini); w = _dinic_path_dfs(flow, result, flag, dist, a.to, i_sink, w); if (w == -1) continue; a.w_max -= w; result[a.to] += w; sumw += w; mini -= w; term = false; flag[u] = false; // TODO: 末尾では? if (mini == 0) return term ? -1 : sumw; } } for (int e : flow.vertex_from[u]) { Flow::Arrow& a = flow.arrow[e]; if (a.cap>a.w_max && dist[u]>dist[a.from]) { int w; if (mini < 0) w = a.cap - a.w_max; else w = min(a.cap - a.w_max, mini); w = _dinic_path_dfs(flow, result, flag, dist, a.from, i_sink, w); if (w == -1) continue; a.w_max += w; result[a.to] -= w; sumw += w; mini -= w; term = false; flag[u] = false; if (mini == 0) return term ? -1 : sumw; } } return term ? -1 : sumw; } // flowは書き換えられる. void dinic(Flow &flow, vector& result, int i_source, int i_sink) { assert(i_source != i_sink); result.resize(flow.n); int distbegin = 0; vector dist(flow.n); queue q; vector flag(flow.n); for (int distbegin = 0; ; distbegin += flow.n) { q.emplace(i_sink); // bfsはsinkからsourceへの距離を計算. dist[i_sink] = distbegin + 1; while (!q.empty()) { int v = q.front(); q.pop(); for (int ie : flow.vertex_from[v]) { const Flow::Arrow& e = flow.arrow[ie]; if (0& result) { int count = 0; vector r; dinic(flow, r, ss, ss + 1); for (Flow::Arrow& a : flow.arrow) { if (a.from == ss || a.to == ss + 1) continue; if (a.w_max == 0) { result.insert(a); count++; } } return count; } }; int width, height; int m, n, kei; int main() { scanner >> n; Flow g(n*2 + 2); int l = n * 2; for (int i = 0; i < n; ++i) { int h = 0; for (int j = 0; j < n; ++j) { int k; scanner >> k; h = max(h, k); if (i == j) continue; g.connect(i, n + j, k); } g.connect(l, i, h); g.connect(n + i, l + 1,h); } vectordam; dinic(g, dam, l, l + 1); cout << dam[l + 1]/2 << endl; return 0; }