Pagini recente » Cod sursa (job #1720801) | Cod sursa (job #2643577) | Cod sursa (job #245167) | Cod sursa (job #1697738) | Cod sursa (job #1217511)
#include<fstream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
ifstream cin("fmcm.in");
ofstream cout("fmcm.out");
const int nmax = 355, inf = 1000000000;
vector <int> g[nmax];
queue<int> q;
int n,m,s,d;
int viz[nmax],p[nmax],dist[nmax];
int c[nmax][nmax],f[nmax][nmax],cost[nmax][nmax];
int BellmanFord()
{
int i;
while (q.size()) q.pop();
q.push(s);
for (i=1;i<=n;i++) viz[i]=0;
viz[s]=1;
for (i=1;i<=n;i++) dist[i]=inf;
dist[s]=0;
while (!q.empty())
{
int v=q.front(); q.pop();
if (v==d) continue;
for (i=0;i<g[v].size();i++)
{
int to=g[v][i], len=cost[v][to];
if (f[v][to]==c[v][to]) continue;
if (dist[v]+len<dist[to])
{
dist[to]=dist[v]+len;
p[to]=v;
q.push(to);
}
}
}
if (dist[d]!=inf) return 1;
return 0;
}
int main()
{
cin>>n>>m>>s>>d;
int i;
for (i=1;i<=m;i++)
{
int x,y,z,t;
cin>>x>>y>>z>>t;
g[x].push_back(y);
g[y].push_back(x);
c[x][y]=z;
cost[x][y]=t;
cost[y][x]=-t;
}
int flow=0,mflow=0;
while (BellmanFord())
{
int j;
mflow=inf;
for(j=d;j!=s;j=p[j])
mflow=min(mflow,c[p[j]][j]-f[p[j]][j]);
for (j=d;j!=s;j=p[j])
{
f[p[j]][j]+=mflow;
f[j][p[j]]-=mflow;
}
flow+=dist[d]*mflow;
}
cout<<flow;
return 0;
}