Pagini recente » Cod sursa (job #155477) | Cod sursa (job #699740) | Cod sursa (job #3293157) | Cod sursa (job #1188853) | Cod sursa (job #2839667)
#include <bits/stdc++.h>
//#pragma GCC optimize ("03")
#define FastIO ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define FILES freopen("cc.in" , "r" , stdin) , freopen("cc.out" , "w" , stdout)
#define ll long long
#define ull unsigned long long
#define ld long double
#define eb emplace_back
#define pb push_back
#define qwerty1 first
#define qwerty2 second
#define qwerty3 -> first
#define qwerty4 -> second
#define umap unordered_map
#define uset unordered_set
#define pii pair < int , int >
#define dbg(x) cerr << #x << ": " << x << '\n'
namespace FastRead
{
char __buff[5000];int __lg = 0 , __p = 0;
char nc()
{
if(__lg == __p){__lg = fread(__buff , 1 , 5000 , stdin);__p = 0;if(!__lg) return EOF;}
return __buff[__p++];
}
template<class T>void read(T&__x)
{
T __sgn = 1; char __c;while(!isdigit(__c = nc()))if(__c == '-')__sgn = -1;
__x = __c - '0';while(isdigit(__c = nc()))__x = __x * 10 + __c - '0';__x *= __sgn;
}
}
using namespace FastRead;
using namespace std;
const int N = 2e2 + 10;
const int M = 1e9 + 7;
const ld PI = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n , sink , source;
int cost[N][N] , capacity[N][N] , d[N] , parent[N];
vector < int > G[N];
priority_queue < pii > pq;
void addEdge(int x , int y , int c , int z)
{
G[x].pb(y);
G[y].pb(x);
capacity[x][y] = c;
capacity[y][x] = 0;
cost[x][y] = z;
cost[y][x] = -z;
}
bool inline djikstra()
{
for(int i = 0 ; i <= 2 * n + 1 ; i++)
d[i] = INT_MAX;
d[0] = 0;
pq.push({0 , 0});
while(!pq.empty())
{
int node = pq.top().second;
int curr_dist = pq.top().first;
pq.pop();
if(d[node] != curr_dist) continue;
for(auto to : G[node])
if(capacity[node][to] && d[to] > d[node] + cost[node][to])
{
d[to] = d[node] + cost[node][to];
parent[to] = node;
pq.push({d[to] , to});
}
}
return d[sink] != INT_MAX;
}
int Flow()
{
int mnCost = 0;
while(djikstra())
{
int mnFlow = INT_MAX;
for(int curr = sink ; curr != source ; curr = parent[curr])
mnFlow = min(mnFlow , capacity[parent[curr]][curr]);
for(int curr = sink ; curr != source ; curr = parent[curr])
{
capacity[parent[curr]][curr] -= mnFlow;
capacity[curr][parent[curr]] += mnFlow;
mnCost += mnFlow * cost[parent[curr]][curr];
}
}
return mnCost;
}
signed main()
{
#ifndef ONLINE_JUDGE
FastIO , FILES;
#endif
int i , j;
cin >> n;
for(i = 1 ; i <= n ; i++)
for(j = 1 ; j <= n ; j++)
cin >> cost[i][j] , addEdge(i , j + n , 1 , cost[i][j]);
for(i = 1 ; i <= n ; i++)
addEdge(0 , i , 1 , 0);
for(i = n + 1 ; i <= 2 * n ; i++)
addEdge(i , 2 * n + 1 , 1 , 0);
source = 0;
sink = 2 * n + 1;
cout << Flow();
return 0;
}