Pagini recente » Cod sursa (job #3042247) | Cod sursa (job #3254490) | Cod sursa (job #227843) | Cod sursa (job #2956110) | Cod sursa (job #297788)
Cod sursa(job #297788)
#include <stdio.h>
#define Lg 355
using namespace std;
struct nod
{
int inf;
nod *next;
}*sir[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 adauga(int a,int b)
{
nod *q=new nod;
q->inf=b;
q->next=sir[a];
sir[a]=q;
}
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);
adauga(x,y);
adauga(y,x);
cap[x][y]=c;
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 (nod *q=sir[N];q;q=q->next)
{
N1=q->inf;
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+=flux*Cost[D];
bell_BMW();
}
}
int main ()
{
citire();
fmcm();
printf("%d\n",rez);
return 0;
}