Pagini recente » Cod sursa (job #1534822) | Cod sursa (job #470522) | Cod sursa (job #1805067) | Cod sursa (job #3270187) | Cod sursa (job #302793)
Cod sursa(job #302793)
#include <stdio.h>
#define Lg 355
using namespace std;
int sir[Lg][Lg];
int grad[Lg];
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 ("fmcm.in","r",stdin);
freopen ("fmcm.out","w",stdout);
int x,y,c,cos;
scanf ("%d %d %d %d\n",&n,&m,&S,&D);
for (int i=0;i<m;i++)
{
scanf ("%d %d %d %d\n",&x,&y,&c,&cos);
cap[x][y]=c;
sir[x][grad[x]++]=y;
sir[y][grad[y]++]=x;
cost[x][y]=cos;
cost[y][x]=-cos;
}
}
void bell_BMW()
{
int N,N1;
for (int i=1;i<=n;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];
for (int q=0;q<grad[N];q++)
{
N1=sir[N][q];
if (cap[N][N1])
if ( 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];
if (cap[T][poz]<flux)
flux=cap[T][poz];
poz=T;
}
poz=D;
while (poz!=S)
{
T=tati[poz];
cap[T][poz]-=flux;
cap[poz][T]+=flux;
poz=T;
}
rez+=flux*Cost[D];
bell_BMW();
}
}
int main ()
{
citire();
fmcm();
printf("%d\n",rez);
return 0;
}