Pagini recente » Cod sursa (job #2947554) | Cod sursa (job #1305105) | Cod sursa (job #2600467) | Cod sursa (job #1059048) | Cod sursa (job #1146131)
#include <fstream>
#include <queue>
#include <vector>
#include <cstring>
#define oo 0x3f3f3f3f
using namespace std;
ifstream fin("fmcm.in");
ofstream fout("fmcm.out");
vector<pair<int,int> >g[355];
queue<int>q;
int dst[355], cst[355], cr[355], t[355], cap[355][355], f[355][355], n, m, s, d, c, z, x, y, sol;
bool vis[355];
bool BellmanFord()
{
memset(vis,0,sizeof(vis));
for(int i = 1; i<= n; i++ )
{
dst[i]=oo;
t[i]=-1;
}
q.push(s);
vis[s]=1;
dst[s]=0;
while(!q.empty())
{
int x=q.front(); q.pop();
for(vector<pair<int,int> >::iterator it=g[x].begin(); it<g[x].end(); it++ )
{
if(dst[it->first]>dst[x]+it->second && cap[x][it->first]-f[x][it->first]>0)
{
dst[it->first]=dst[x]+it->second;
t[it->first]=x;
if(!vis[it->first])
{
vis[it->first]=1;
q.push(it->first);
}
}
}
}
return (dst[d]!=oo);
}
int main()
{
fin>>n>>m>>s>>d;
for(int i = 1; i<= m; i++ )
{
fin>>x>>y>>c>>z;
g[x].push_back(make_pair(y,z));
g[y].push_back(make_pair(x,-z));
cap[x][y]=c;
}
while(BellmanFord())
{
int flux=oo;
for(int x = d; x!=s; x=t[x])
flux=min(flux,cap[t[x]][x]-f[t[x]][x]);
sol+=flux*dst[d];
for(int x = d; x!=s; x=t[x])
{
f[t[x]][x]+=flux;
f[x][t[x]]-=flux;
}
}
fout<<sol;
fin.close();
fout.close();
return 0;
}