Pagini recente » Cod sursa (job #1183973) | Cod sursa (job #863869) | Cod sursa (job #2531944) | Cod sursa (job #2003323) | Cod sursa (job #342261)
Cod sursa(job #342261)
#include <iostream>
#include <queue>
using namespace std;
#define MAXN 202
const int oo=(int)1e9;
int n,cost[MAXN][MAXN],res,dist[MAXN],cyc[MAXN],to[MAXN],src,sink,cap[MAXN][MAXN],flow[MAXN][MAXN];
bool inQ[MAXN];
int bellman(){
for (int i=1;i<=sink;i++){
dist[i]=oo;
inQ[i]=0;
cyc[i]=0;
}
queue<int>q;
q.push(0);
inQ[0]=1;
dist[0]=0;
while (!q.empty()){
int top=q.front();
q.pop();
inQ[top]=0;
if (cyc[top]==n) return oo;
for (int i=0;i<=sink;i++){
if (cap[top][i]-flow[top][i]>0){
if (dist[i]>dist[top]+cost[top][i]){
dist[i]=dist[top]+cost[top][i];
to[i]=top;
if (inQ[i]==0){
q.push(i);
}
}
}
}
}
return dist[sink];
}
void mincut(){
int cnt=0,d,mini;
while ((d=bellman())!=oo){
mini=oo;
for (int i=sink;i!=src;i=to[i]){
mini=min(mini,cap[to[i]][i]-flow[to[i]][i]);
}
for (int i=sink;i!=src;i=to[i]){
flow[to[i]][i]+=mini;
flow[i][to[i]]-=mini;
}
cnt+=mini*d;
}
res=cnt;
}
void open(){
freopen("cc.in","r",stdin);
freopen("cc.out","w",stdout);
}
int main(){
open();
scanf("%d",&n);
src=0;sink=2*n+1;
for (int i=1;i<=n;i++){
cap[0][i]=1;
for (int j=1;j<=n;j++){
scanf("%d",&cost[i][j+n]);
cost[j+n][i]=-cost[i][j+n];
cap[i][j+n]=1;
cap[j+n][sink]=1;
}
}
res=0;
mincut();
printf("%d\n",res);
//system("pause");
return 0;
}