Pagini recente » Istoria paginii runda/pnb22/clasament | Istoria paginii runda/cls_9_simulare_oji/clasament | Istoria paginii runda/testere21/clasament | Istoria paginii runda/dadanuda/clasament | Cod sursa (job #2017718)
#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 dreal1[nmax], dreal2[nmax], dfictiv[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 initb()
{
for(int i=1; i<=n; i++)
dreal1[i]=inf;
prim=1;
ultim=1;
k=1;
coada[prim]=s;
viz[s]=1;
dreal1[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)&&(dreal1[vec]>= dreal1[nod]+cost[nod][vec]))
{
dreal1[vec]= dreal1[nod]+cost[nod][vec];
pred[vec]=nod;
if(!viz[vec])
{
viz[vec]=1;
ultim++;
k++;
coada[ultim]=vec;
}
}
}
}
if(dreal1[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+=dreal2[d]*fmin;
}
void dijkstra()
{
int vfmin, i, j, vec;
long int mini=inf;
memset(viz, 0, sizeof(viz));
for(i=1; i<=n; i++)
dfictiv[i]=inf;
dfictiv[s]=0;
dreal2[s]=0;
for(i=1; i<=n; i++)
{
mini=inf;
vfmin=0;
for(j=1; j<=n; j++)
if((!viz[j])&&(mini> dfictiv[j])) {mini=dfictiv[j]; vfmin=j;}
if(vfmin)
{
viz[vfmin]=1;
for(j=cap[vfmin]; j<cap[vfmin+1]; j++)
if(ca[vfmin][v[j]]>0)
{
vec=v[j];
if(dfictiv[vec]> dfictiv[vfmin]+cost[vfmin][vec]+ dreal1[vfmin]-dreal1[vec])
{
pred[vec]=vfmin;
dfictiv[vec]=dfictiv[vfmin]+cost[vfmin][vec]+ dreal1[vfmin]-dreal1[vec];
dreal2[vec]=dreal2[vfmin]+cost[vfmin][vec];
}
}
}
}
memcpy(dreal1, dreal2, sizeof(dreal1));
if(dfictiv[d]==inf) ok=0;
}
int main()
{
citire();
initb();
bellman();
while(ok)
{
dijkstra();
if(ok) actual();
}
f2<<fmax;
return 0;
}