Pagini recente » redsnow_3 | Cod sursa (job #1556989) | Cod sursa (job #2783238) | Istoria paginii runda/iconcurs12/clasament | Cod sursa (job #2017556)
#include <fstream>
#include <string.h>
#include <stdlib.h>
#define nmax 355
#define mmax 12505
#define inf 125050000
using namespace std;
fstream f1("fmcm.in", ios::in);
fstream f2("fmcm.out", ios::out);
int n, m, s, d, ok=1;
int aux[nmax], cap[nmax], cost[nmax][nmax], ca[nmax][nmax], v[mmax*2], viz[nmax], coada[nmax], prim, ultim, k, pred[nmax];
long int dist[nmax], fmax;
struct muchii
{
int x, y;
}muc[mmax];
void citire()
{
int x, y, c, i,CA;
f1>>n>>m>>s>>d;
for(i=1; i<=m; i++)
{
f1>>muc[i].x>>muc[i].y>>CA>>c;
aux[muc[i].x]++;
aux[muc[i].y]++;
cost[muc[i].x][muc[i].y]=c;
cost[muc[i].y][muc[i].x]=-c;
ca[muc[i].x][muc[i].y]=CA;
}
cap[1]=1;
for(i=2; i<=n+1; i++)
{
cap[i]=cap[i-1]+aux[i-1];
aux[i-1]=cap[i-1];
}
for(i=1; i<=m; i++)
{
x=muc[i].x;
y=muc[i].y;
v[aux[x]]=y;
aux[x]++;
v[aux[y]]=x;
aux[y]++;
}
}
void init()
{
memset(viz, 0, sizeof(viz));
memset(pred, 0, sizeof(pred));
for(int i=1; i<=n; i++)
dist[i]=inf;
prim=1;
ultim=1;
k=1;
coada[prim]=s;
viz[s]=1;
dist[s]=0;
}
void bellman()
{
int nod, vec, i;
while(k!=0)
{
nod=coada[prim];
viz[nod]=0;
prim++;
k--;
for(i=cap[nod]; i<cap[nod+1]; i++)
{
vec=v[i];
if((ca[nod][vec]>0)&&(dist[vec]> dist[nod]+cost[nod][vec]))
{
dist[vec]= dist[nod]+cost[nod][vec];
pred[vec]=nod;
if(!viz[vec])
{
viz[vec]=1;
ultim++;
k++;
coada[ultim]=vec;
}
}
}
}
if(dist[d]!=inf) ok=1;
else ok=0;
}
void actual()
{
int nod;
long int fmin=inf;
nod=d;
while(pred[nod])
{
if(fmin> ca[pred[nod]][nod]) fmin= ca[pred[nod]][nod];
nod=pred[nod];
}
nod=d;
while(pred[nod])
{
ca[pred[nod]][nod]-=fmin;
ca[nod][pred[nod]]+=fmin;
nod=pred[nod];
}
fmax+=dist[d]*fmin;
}
int main()
{
citire();
while(1)
{
init();
bellman();
if(ok) actual();
else break;
}
f2<<fmax;
return 0;
}