Problem:
Problem H:
Sightseeing Tour
KM city has $N$ sightseeing areas. Currently every pair of area is connected by a bidirectional road.
However for some reason, Mr. KM, the mayor of this city, decided to make all of these roads one-way . It costs $C_{i, j}$ dollars to renovate the road between area $i$ and area $j$ to a one-way road from area $i$ to area $j$. Of course, Mr. KM is economic and wants to minimize the total cost of the renovation.
On the other hand, because tourism is the most important industry for KM city, there must exists a tour that goes through all the sightseeing areas, visiting each area exactly once. The first and last area of the path need not to be the same. Can you calculate the minimum total cost required for the renovation, given this situation?
Input
The first line contains the number of sightseeing area $N$ ($1 \leq N \leq 100$). Next $N$ lines describe the integer matrix $C$, where the $j$-th element of the $i$-th row of the input describes $C_{i,j}$ ($0 \leq C_{i,j} \leq 1,000,000$). For each $i$, you can assume $C_{i,i}$ is always zero.
Output
Output the minimum cost in a line.
Sample Input 1
3
0 2 7
2 0 4
5 8 0
Sample Output 1
11

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

struct edge {
    int to, cost;
    edge(){}
    edge(int _to, int _cost) : to(_to), cost(_cost) {}
};

int N;
int mi_len;
int g;
vector<vector<edge>> G;
vector<bool> visited;
void dfs(int now, int cnt_visited, int len /*, vector<bool> visited, vector<int> path */) {

    if (visited[now]) return;
    cnt_visited++;
    visited[now] = true;
    // path.emplace_back(now);

    if (now == g) {
        if (cnt_visited == N && len < mi_len) {
            mi_len = len;
            // opt_path = path;
        }
        visited[now] = false;
        return;
    }

    for (auto e : G[now]) {
        // dfs(nxt, cnt_visited, len + cost, visited, path);
        dfs(e.to, cnt_visited, len + e.cost);
        visited[e.to] = false;
    }
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    cin >> N;
    VV d(N, V(N));
    rep(i, N) rep(j, N) cin >> d[i][j];

    int sm = 0;
    G.resize(N);
    rep(i, N) {
        rep(j, i) {
            sm += min(d[i][j], d[j][i]);
            if (d[i][j] <= d[j][i]) {
                G[i].emplace_back(j, 0);
                G[j].emplace_back(i, d[j][i] - d[i][j]);
            } else {
                G[j].emplace_back(i, 0);
                G[i].emplace_back(j, d[i][j] - d[j][i]);
            }
        }
    }

    int mi_diff = 1e15;
    rep(i, N) {
        rep(j, N) {
            if (i == j) continue;
            mi_len = 1e15;
            visited.resize(N, false);
            dfs(i, 0, 0);
            mi_diff = min(mi_diff, mi_len);
        }
    }

    cout << sm + mi_diff << endl;

}
accuracy: 0.03125
time_ms: 10
memory_kb: 19152
final_rank: 3

code_snippet: #include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cassert>
#include <string>
#include <memory.h>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <cctype>
#include <fstream>
#include <cmath>
using namespace std;

#define REP2(i, m, n) for(ll i = (ll)(m); i < (ll)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ITER(c) __typeof((c).begin())
#define PB(e) push_back(e)
#define FOREACH(i, c) for(ITER(c) i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define PARITY(n) ((n) & 1)

typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;

class MinCostFlow{
  typedef pair<ll, ll> P;
  class edge{
  public:
    ll to, cap, cost, rev;
    edge(){}
    edge(ll to, ll cap, ll cost, ll rev) : to(to),cap(cap),cost(cost),rev(rev){}
  };
  
  ll infinity;
  ll SIZE;
  vector<ll> dist;
  vector<ll> h;
  vector<ll> prevv;
  vector<ll> preve;
  vector<vector<edge> > G;
public:
  MinCostFlow(ll V) : SIZE(V),dist(vector<ll>(V)),h(vector<ll>(V)),
		       prevv(vector<ll>(V)), preve(vector<ll> (V)), 
		       G(vector<vector<edge> >(V)){ 
    infinity = 1000 * 1000 * 1000 + 7;
  }
  void add_edge(ll from, ll to, ll cap, ll cost){
    G[from].push_back(edge(to, cap, cost, G[to].size()));
    G[to].push_back(edge(from, 0, -cost, G[from].size()-1));
  }
  void init(ll size = 0){
    if(size > 0){
      dist.resize(size);
      h.resize(size);
      prevv.resize(size);
      preve.resize(size);
      G.resize(size);
    }
    for(ll i = 0; i < (ll)G.size(); i++) G[i].clear();
  }
  
  ll min_cost_flow(ll s, ll t, ll f){
    ll res = 0;
    fill(h.begin(), h.end(), 0);
    while(f > 0){
      priority_queue<P, vector<P>, greater<P> > que;
      fill(dist.begin(), dist.end(), infinity);
      dist[s] = 0;
      que.push(P(0, s));
      while(!que.empty()){
	P p = que.top(); que.pop();
	ll v  = p.second;
	if(dist[v] < p.first) continue;
	for(ll i =  0; i < (ll)G[v].size(); i++){
	  edge &e = G[v][i];
	  if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){
	    dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
	    prevv[e.to] = v;
	    preve[e.to] = i;
	    que.push(P(dist[e.to], e.to));
	  }
	}
      }
      
      if(dist[t] == infinity) return -1;
      for(ll v = 0; v < SIZE; v++) h[v] += dist[v];
      
      ll d = f;
      for(ll v = t; v != s; v = prevv[v]){
	d = min(d, G[prevv[v]][preve[v]].cap);
      }
      f -= d;
      res += d * h[t];
      for(ll v = t; v != s; v = prevv[v]){
	edge &e = G[prevv[v]][preve[v]];
	e.cap -= d;
	G[v][e.rev].cap += d;
      }
    }
    return res;
  }
};


int main(){
  ll n;
  cin >> n;
  vector<vector<ll> > C(n, vector<ll>(n, 0));
  REP(i, n) REP(j, n) cin >> C[i][j];
  ll s = 2 * n;
  ll t = s + 1;
  MinCostFlow mcf(2 * n + 2);
  ll S = 0;

  REP(i, n)REP(j, i){
    ll tmp = min(C[i][j], C[j][i]);
    S += tmp;
    C[i][j] -= tmp;
    C[j][i] -= tmp;
  }

  REP(i, n){
    mcf.add_edge(s, i, 1, 0);
    mcf.add_edge(i + n, t, 1, 0);
  }
  
  REP(i, n)REP(j, n){
    mcf.add_edge(i, j + n, 1, C[i][j]);
  }
  cout << S + mcf.min_cost_flow(s, t, n - 1) << endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 2128
final_rank: 1

code_snippet: #include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cassert>
#include <string>
#include <memory.h>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <cctype>
#include <fstream>
#include <cmath>
using namespace std;

#define REP2(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ITER(c) __typeof((c).begin())
#define PB(e) push_back(e)
#define FOREACH(i, c) for(ITER(c) i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define PARITY(n) ((n) & 1)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;

class MinCostFlow{
  typedef pair<int, int> P;
  class edge{
  public:
    int to, cap, cost, rev;
    edge(){}
    edge(int to, int cap, int cost, int rev) : to(to),cap(cap),cost(cost),rev(rev){}
  };
  
  int infinity;
  int SIZE;
  vector<int> dist;
  vector<int> h;
  vector<int> prevv;
  vector<int> preve;
  vector<vector<edge> > G;
public:
  MinCostFlow(int V) : SIZE(V),dist(vector<int>(V)),h(vector<int>(V)),
		       prevv(vector<int>(V)), preve(vector<int> (V)), 
		       G(vector<vector<edge> >(V)){ 
    infinity = 1000 * 1000 * 1000 + 7;
  }
  void add_edge(int from, int to, int cap, int cost){
    G[from].push_back(edge(to, cap, cost, G[to].size()));
    G[to].push_back(edge(from, 0, -cost, G[from].size()-1));
  }
  void init(int size = 0){
    if(size > 0){
      dist.resize(size);
      h.resize(size);
      prevv.resize(size);
      preve.resize(size);
      G.resize(size);
    }
    for(int i = 0; i < (int)G.size(); i++) G[i].clear();
  }
  
  int min_cost_flow(int s, int t, int f){
    int res = 0;
    fill(h.begin(), h.end(), 0);
    while(f > 0){
      priority_queue<P, vector<P>, greater<P> > que;
      fill(dist.begin(), dist.end(), infinity);
      dist[s] = 0;
      que.push(P(0, s));
      while(!que.empty()){
	P p = que.top(); que.pop();
	int v  = p.second;
	if(dist[v] < p.first) continue;
	for(int i =  0; i < (int)G[v].size(); i++){
	  edge &e = G[v][i];
	  if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){
	    dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
	    prevv[e.to] = v;
	    preve[e.to] = i;
	    que.push(P(dist[e.to], e.to));
	  }
	}
      }
      
      if(dist[t] == infinity) return -1;
      for(int v = 0; v < SIZE; v++) h[v] += dist[v];
      
      int d = f;
      for(int v = t; v != s; v = prevv[v]){
	d = min(d, G[prevv[v]][preve[v]].cap);
      }
      f -= d;
      res += d * h[t];
      for(int v = t; v != s; v = prevv[v]){
	edge &e = G[prevv[v]][preve[v]];
	e.cap -= d;
	G[v][e.rev].cap += d;
      }
    }
    return res;
  }
};


int main(){
  int n;
  cin >> n;
  vector<vector<int> > C(n, vector<int>(n, 0));
  REP(i, n) REP(j, n) cin >> C[i][j];
  int s = 2 * n;
  int t = s + 1;
  MinCostFlow mcf(2 * n + 2);
  int S = 0;

  REP(i, n)REP(j, i){
    int tmp = min(C[i][j], C[j][i]);
    S += tmp;
    C[i][j] -= tmp;
    C[j][i] -= tmp;
  }

  REP(i, n){
    mcf.add_edge(s, i, 1, 0);
    mcf.add_edge(i + n, t, 1, 0);
  }
  
  REP(i, n)REP(j, n){
    mcf.add_edge(i, j + n, 1, C[i][j]);
  }
  cout << S + mcf.min_cost_flow(s, t, n - 1) << endl;
  return 0;
}
accuracy: 0.21875
time_ms: 10
memory_kb: 1532
final_rank: 2