Pagini recente » Cod sursa (job #2604349) | Cod sursa (job #2284935) | Cod sursa (job #3246615) | Cod sursa (job #1829716) | Cod sursa (job #711719)
Cod sursa(job #711719)
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("fmcm.in");
ofstream g("fmcm.out");
typedef struct nod
{
int inf;
nod *urm;
} NOD;
int G[360][360],C[360][360];
int n,m,S,D;
int t[360],v[360],d[360],inq[360];
int cost,maxFlow;
void citire()
{
f>>n>>m>>S>>D;
int x,y,c,z;
while(m--)
{
f>>x>>y>>c>>z;
G[x][y]=c;
C[x][y]=z;
C[y][x]=-z;
}
f.close();
}
void bellmanFord()
{
int x=S;
NOD *queue=new NOD;
queue->inf=x;queue->urm=NULL;
memset(d,0,sizeof(d));
memset(t,0,sizeof(t));
memset(inq,0,sizeof(inq));
for(NOD *p=queue,*aux;p;aux=p,p=p->urm,delete aux)
{
int i=p->inf;
for(int j=1;j<=n;j++)
{
if(G[i][j]>0&&(d[i]+C[i][j]<d[j]||!d[j])&&j!=S)
{
d[j]=d[i]+C[i][j];
t[j]=i;
if(!inq[j])
{
NOD *q=new NOD;
q->inf=j;q->urm=NULL;queue->urm=q;queue=q;
inq[j]=1;
}
}
}
inq[i]=0;
}
}
int main()
{
citire();
bellmanFord();
while(d[D])
{
int minn=2000000000;
for(int i=D;t[i];i=t[i])
if(G[t[i]][i]<minn)
minn=G[t[i]][i];
cost+=minn*d[D];
for(int i=D;t[i];i=t[i])
if(G[t[i]][i]>0)
{
G[t[i]][i]-=minn;
G[i][t[i]]+=minn;
}
bellmanFord();
}
g<<cost<<'\n';
g.close();
return 0;
}