Pagini recente » tema | Cod sursa (job #1607011) | Cod sursa (job #2998937) | Cod sursa (job #323646) | Cod sursa (job #1972908)
#include <fstream>
#include <vector>
#include <cstring>
#define Nmax 351
#define inf 1e9
#define pb push_back
using namespace std;
ifstream f("fmcm.in");
ofstream g("fmcm.out");
int n,m,S,D,x,y,ant[Nmax],fw[Nmax][Nmax],c[Nmax][Nmax],mn[Nmax];
vector<int> V[Nmax],C;
bool inQ[Nmax],ok;
bool bfs()
{
C.clear();
C.pb(S);
ok = 0;
memset(inQ,0,sizeof(inQ));
inQ[S] = 1;
inQ[D] = 1;
memset(mn,0x3f,sizeof(mn));
for (int i=0;i<C.size();i++)
{
int now = C[i];
inQ[i] = 0;
for (int j=0;j<V[now].size();j++)
{
if (fw[now][V[now][j]]!=0)
{
if (mn[V[now][j]]>mn[now] + c[now][V[now][j]])
{
ant[V[now][j]] = now;
mn[V[now][j]] = mn[now] + c[now][V[now][j]];
if (V[now][j] == D)
ok = 1;
if (!inQ[V[now][j]])
{
C.pb(V[now][j]);
inQ[V[now][j]] = 1;
}
}
}
}
}
return ok;
}
int main()
{
f>>n>>m>>S>>D;
for (int i=1;i<=m;i++)
{
f>>x>>y;
f>>fw[x][y];
f>>c[x][y];
c[y][x] = -c[x][y];
V[x].pb(y);
V[y].pb(x);
}
int rez = 0;
while (bfs())
{
int s = 0,nod = D,mx = inf;
while (nod!=S)
{
mx = min(mx,fw[ant[nod]][nod]);
s+=c[ant[nod]][nod];
nod = ant[nod];
}
rez += s * mx;
nod = D;
while (nod!=S)
{
fw[ant[nod]][nod]-=mx;
fw[nod][ant[nod]]+=mx;
nod = ant[nod];
}
}
g<<rez;
return 0;
}