Pagini recente » Cod sursa (job #1127938) | Cod sursa (job #169680) | Cod sursa (job #2875751) | Cod sursa (job #1373589) | Cod sursa (job #273352)
Cod sursa(job #273352)
#include<fstream.h>
#define nx 220
#define inf 20000000
int cost[nx][nx],d[nx],heap[nx],pos[nx],tat[nx],cap[nx][nx];
int n,m,drum,s,S,D,L,flux[nx][nx];
void up_heap(int x)
{
int aux;
while (x>>1 && d[heap[x]]<d[heap[x>>1]])
{
aux=heap[x];
heap[x]=heap[x>>1];
heap[x>>1]=aux;
x>>=2;
}
}
void down_heap(int x)
{
int y=0,aux;
while (x!=y)
{
y=x;
if (y*2<=L && d[heap[x]]>d[heap[y*2]]) x = y*2;
if (y*2+1 <= L && d[heap[x]]>d[heap[y*2+1]]) x = y*2+1;
aux = heap[x], heap[x] = heap[y], heap[y] = aux;
}
}
int dijkstra()
{
int i,j,r;
for (i=0;i<D;++i)
for (j=i+1;j<=D;++j)
if (d[i]!=inf && d[j]!=inf)
{
cost[i][j]+=d[i]-d[j];
cost[j][j]+=d[j]-d[i];
}
for (i=0;i<=D;++i)
{
d[i]=inf;
tat[i]=-1;
heap[i]=i;
pos[i]=i;
}
heap[1]=S;
L=1;d[S]=0;
while (L && d[heap[1]]!=inf)
{
for (i=1;i<=D;++i)
{
if (cap[heap[1]][i]>flux[heap[1]][i] && d[i]>d[heap[1]]+cost[heap[1]][i])
{
d[i]=d[heap[1]]+cost[heap[1]][i];
tat[i]=heap[1];
heap[++L]=i;
up_heap(i);
}
}
heap[1]=heap[L--];
pos[heap[1]]=1;
down_heap(1);
}
if (d[D]!=inf)
{
drum=1;
for (i=D;i!=S;i=tat[i])
{
flux[tat[i]][i]++;
flux[i][tat[i]]--;
}
s+=d[D];
return s;
}
return 0;
}
int Flux()
{
drum=1;
int rez=0;
while (drum)
{
drum=0;
rez+=dijkstra();
}
return rez;
}
int main()
{
ifstream be ("cc.in");
ofstream ki ("cc.out");
int i,j;
be>>n;
for (i=1;i<=n;++i)
for (j=1;j<=n;++j)
{
be>>cost[i][n+j];
// cost[n+j][i]=-cost[i][n+j];
cap[i][n+j]=1;
}
be.close();
S=0,D=2*n+1;
for (i=1;i<=n;++i)
{
cost[n+i][D]=0;
cap[n+i][D]=1;
cost[S][i]=0;
cap[S][i]=1;
}
ki<<Flux()<<'\n';
ki.close();
return 0;
}