Pagini recente » Cod sursa (job #2041024) | Cod sursa (job #1667900) | Cod sursa (job #496251) | Cod sursa (job #1018087) | Cod sursa (job #297820)
Cod sursa(job #297820)
#include <stdio.h>
#define Lg 110
using namespace std;
int cap[Lg][Lg],cost[Lg][Lg];
int n,m,S,D,rez,inc,sf,Cost_aux;
int Q[Lg*Lg],tati[Lg],Cost[Lg];
char viz[Lg];
void citire()
{
freopen ("cc.in","r",stdin);
freopen ("cc.out","w",stdout);
scanf ("%d",&n);
S=0;
D=2*n+1;
for (int i=1;i<=n;i++)
{
cap[S][i]=1;
for (int j=n+1;j<=n+n;j++)
{
scanf ("%d",&cost[i][j]);
cost[j][i]=-cost[i][j];
cap[j][D]=1;
cap[i][j]=1;
}
}
}
void bell_BMW()
{
int N,N1,in,ssf;
for (int i=0;i<=2*n+1;i++)
Cost[i]=6000000LL;
inc=0;
sf=1;
Q[inc]=S;
Cost[S]=0;
viz[S]=1;
while (inc<sf)
{
N=Q[inc++];
viz[N]=0;
Cost_aux=Cost[N];
int in=n+1,ssf=2*n+1;
if (N==S)
{
in=1;
ssf=n;
}
for (N1=in;N1<=ssf;N1++)
{
if (cap[N][N1] && Cost[N1]>=Cost_aux+cost[N][N1])
{
Cost[N1]=Cost_aux+cost[N][N1];
tati[N1]=N;
if (!viz[N1])
{
viz[N1]=1;
Q[sf++]=N1;
}
}
}
}
}
void fmcm()
{
int flux,poz,T;
bell_BMW();
while (Cost[D]!=6000000LL)
{
flux=6000000LL;
poz=D;
while (poz!=S)
{
T=tati[poz];
flux=(flux>cap[T][poz])?cap[T][poz]:flux;
poz=T;
}
poz=D;
while (poz!=S)
{
T=tati[poz];
cap[T][poz]-=flux;
cap[poz][T]+=flux;
poz=T;
}
rez+=Cost[D];
bell_BMW();
}
}
int main ()
{
citire();
fmcm();
printf("%d\n",rez);
return 0;
}