This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/assignment"
#include "../default/t.cpp"
#include "../graph/MCMF.cpp"
signed main() {
ios::sync_with_stdio(false), cin.tie(NULL);
int n; cin >> n;
vector a(n, vector<int>(n));
for(auto &v : a)
for(int &x : v)
cin >> x;
const int s = 2 * n, t = 2 * n + 1;
MCMF<int, ll> mcmf(2 * n + 2);
for(int v = 0; v < n; v++) {
mcmf.addEdge(s, v, 1, 0);
mcmf.addEdge(v + n, t, 1, 0);
}
for(int u = 0; u < n; u++)
for(int v = 0; v < n; v++)
mcmf.addEdge(u, v + n, 1, a[u][v]);
mcmf.initPotential(s);
cout << mcmf.runFlow(s, t, true).second << '\n';
for(int v = 0; v < n; v++)
for(auto e : mcmf.g[v])
if (e.cap == 0 and e.to != s)
cout << e.to - n << " \n"[v + 1 == n];
return 0;
}
#line 1 "test/assignment.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/assignment"
#line 1 "default/t.cpp"
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>
#include <bit>
#include <compare>
#include <concepts>
#include <numbers>
#include <ranges>
#include <span>
#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)
#define clock chrono::steady_clock::now().time_since_epoch().count()
using namespace std;
template<class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2> pr) {
return os << pr.first << ' ' << pr.second;
}
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
for(size_t i = 0; T x : arr) {
os << x;
if (++i != N) os << ' ';
}
return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
for(size_t i = 0; T x : vec) {
os << x;
if (++i != size(vec)) os << ' ';
}
return os;
}
template<class T>
ostream& operator<<(ostream& os, const set<T> &s) {
for(size_t i = 0; T x : s) {
os << x;
if (++i != size(s)) os << ' ';
}
return os;
}
template<class T1, class T2>
ostream& operator<<(ostream& os, const map<T1, T2> &m) {
for(size_t i = 0; pair<T1, T2> x : m) {
os << x;
if (++i != size(m)) os << ' ';
}
return os;
}
#ifdef DEBUG
#define dbg(...) cerr << '(', _do(#__VA_ARGS__), cerr << ") = ", _do2(__VA_ARGS__)
template<typename T> void _do(T &&x) { cerr << x; }
template<typename T, typename ...S> void _do(T &&x, S&&...y) { cerr << x << ", "; _do(y...); }
template<typename T> void _do2(T &&x) { cerr << x << endl; }
template<typename T, typename ...S> void _do2(T &&x, S&&...y) { cerr << x << ", "; _do2(y...); }
#else
#define dbg(...)
#endif
using ll = long long;
using ull = unsigned long long;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using max_heap = priority_queue<T>;
template<ranges::forward_range rng, class T = ranges::range_value_t<rng>, class OP = plus<T>>
void pSum(rng &v) {
if (!v.empty())
for(T p = v[0]; T &x : v | views::drop(1))
x = p = OP()(p, x);
}
template<ranges::forward_range rng, class T = ranges::range_value_t<rng>, class OP>
void pSum(rng &v, OP op) {
if (!v.empty())
for(T p = v[0]; T &x : v | views::drop(1))
x = p = op(p, x);
}
template<ranges::forward_range rng>
void Unique(rng &v) {
ranges::sort(v);
v.resize(unique(v.begin(), v.end()) - v.begin());
}
template<ranges::random_access_range rng>
rng invPerm(rng p) {
rng ret = p;
for(int i = 0; i < ssize(p); i++)
ret[p[i]] = i;
return ret;
}
template<ranges::random_access_range rng, ranges::random_access_range rng2>
rng Permute(rng v, rng2 p) {
rng ret = v;
for(int i = 0; i < ssize(p); i++)
ret[p[i]] = v[i];
return ret;
}
template<bool directed>
vector<vector<int>> readGraph(int n, int m, int base) {
vector<vector<int>> g(n);
for(int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
u -= base, v -= base;
g[u].emplace_back(v);
if constexpr (!directed)
g[v].emplace_back(u);
}
return g;
}
template<class T>
void setBit(T &msk, int bit, bool x) {
msk = (msk & ~(T(1) << bit)) | (T(x) << bit);
}
template<class T> void flipBit(T &msk, int bit) { msk ^= T(1) << bit; }
template<class T> bool getBit(T msk, int bit) { return msk >> bit & T(1); }
template<class T>
T floorDiv(T a, T b) {
if (b < 0) a *= -1, b *= -1;
return a >= 0 ? a / b : (a - b + 1) / b;
}
template<class T>
T ceilDiv(T a, T b) {
if (b < 0) a *= -1, b *= -1;
return a >= 0 ? (a + b - 1) / b : a / b;
}
template<class T> bool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T> bool chmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
#line 1 "graph/MCMF.cpp"
template<class capT, class cosT>
struct MCMF {
struct edge {
int to, rev;
capT cap;
cosT cos;
edge(int _to, capT _cap, cosT _cos, int _rev) :
to(_to), rev(_rev), cap(_cap), cos(_cos) {}
};
int n;
const capT CAP_MAX = numeric_limits<capT>::max();
const cosT COS_MAX = numeric_limits<cosT>::max();
vector<vector<edge>> g;
vector<int> par, idx;
vector<capT> f;
vector<cosT> pot, dis;
MCMF(int _n) : n(_n), g(n), par(n),
idx(n), f(n), pot(n), dis(n) {}
void addEdge(int from, int to, capT cap, cosT cos) {
g[from].emplace_back(to, cap, cos, ssize(g[to]));
g[to].emplace_back(from, 0, -cos, ssize(g[from]) - 1);
}
void initPotential(int s) {
fill(dis.begin(), dis.end(), COS_MAX);
dis[s] = 0;
for(int i = 1; i < n; i++) {
for(int v = 0; v < n; v++) {
if (dis[v] == COS_MAX) continue;
for(edge e : g[v])
if (e.cap != 0 and dis[v] + e.cos < dis[e.to])
dis[e.to] = dis[v] + e.cos;
}
}
pot.swap(dis);
}
void initPotentialDAG(int s) {
fill(dis.begin(), dis.end(), COS_MAX);
dis[s] = 0;
vector<int> topo = [&]() {
vector<int> topo;
vector<bool> vis(n, false);
auto dfs = [&](int v, auto &&self) -> void {
vis[v] = true;
for(edge e : g[v])
if (e.cap != 0 and !vis[e.to])
self(e.to, self);
topo.emplace_back(v);
};
for(int v = 0; v < n; v++)
if (!vis[v])
dfs(v, dfs);
return topo;
}();
for(int v : topo | views::reverse)
if (dis[v] != COS_MAX)
for(edge e : g[v])
if (e.cap != 0)
chmin(dis[e.to], dis[v] + e.cos);
pot.swap(dis);
}
pair<capT, cosT> runFlow(int s, int t, bool dense = false) {
cosT cost = 0;
capT flow = 0;
while(true) {
fill(dis.begin(), dis.end(), COS_MAX);
dis[s] = 0, f[s] = CAP_MAX;
if (dense) {
vector<bool> vis(n, false);
for(int i = 0; i < n; i++) {
int v = -1;
for(int j = 0; j < n; j++)
if (!vis[j] and (v == -1 or dis[j] < dis[v]))
v = j;
if (v == -1 or dis[v] == COS_MAX) break;
vis[v] = true;
for(edge e : g[v]) {
if (e.cap == 0) continue;
if (cosT x = dis[v] + e.cos + pot[v] - pot[e.to]; x < dis[e.to]) {
dis[e.to] = x, f[e.to] = min(f[v], e.cap);
par[e.to] = v, idx[e.to] = g[e.to][e.rev].rev;
}
}
}
} else {
using T = pair<cosT, int>;
priority_queue<T, vector<T>, greater<T>> pq;
pq.push(make_pair(dis[s], s));
while(!pq.empty()) {
auto [d, v] = pq.top(); pq.pop();
if (dis[v] != d) continue;
for(edge e : g[v]) {
if (e.cap == 0) continue;
if (cosT x = dis[v] + e.cos + pot[v] - pot[e.to]; x < dis[e.to]) {
dis[e.to] = x, f[e.to] = min(f[v], e.cap);
par[e.to] = v, idx[e.to] = g[e.to][e.rev].rev;
pq.push(make_pair(dis[e.to], e.to));
}
}
}
}
if (dis[t] == COS_MAX) break;
int v = t;
while(v != s) {
edge &e = g[par[v]][idx[v]];
e.cap -= f[t], g[v][e.rev].cap += f[t];
v = par[v];
}
flow += f[t], cost += f[t] * (dis[t] - pot[s] + pot[t]);
for(int i = 0; i < n; i++)
if (dis[i] != COS_MAX)
dis[i] += pot[i] - pot[s];
pot.swap(dis);
}
return {flow, cost};
}
};
#line 5 "test/assignment.test.cpp"
signed main() {
ios::sync_with_stdio(false), cin.tie(NULL);
int n; cin >> n;
vector a(n, vector<int>(n));
for(auto &v : a)
for(int &x : v)
cin >> x;
const int s = 2 * n, t = 2 * n + 1;
MCMF<int, ll> mcmf(2 * n + 2);
for(int v = 0; v < n; v++) {
mcmf.addEdge(s, v, 1, 0);
mcmf.addEdge(v + n, t, 1, 0);
}
for(int u = 0; u < n; u++)
for(int v = 0; v < n; v++)
mcmf.addEdge(u, v + n, 1, a[u][v]);
mcmf.initPotential(s);
cout << mcmf.runFlow(s, t, true).second << '\n';
for(int v = 0; v < n; v++)
for(auto e : mcmf.g[v])
if (e.cap == 0 and e.to != s)
cout << e.to - n << " \n"[v + 1 == n];
return 0;
}