Pagini recente » Cod sursa (job #2430505) | Cod sursa (job #3000592) | Cod sursa (job #2434662) | Cod sursa (job #596224) | Cod sursa (job #781722)
Cod sursa(job #781722)
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
#define Max 351
#define Inf 0xffffff
vector<int>g[Max];
set<int>b,v;
int n,s,d,dst[Max],c[Max][Max],pr[Max][Max],f[Max][Max],tt[Max];
bool was[Max];
bool bfs(){
int p,u,x,y;
for(int i=1;i<=n;i++)
{
was[i] = 0;
dst[i] = Inf;
}
was[s] = 1;
dst[s] = 0;
b.clear(); v.clear();
b.insert(s);
for(int k=1;k<=n;k++)
{
while(b.size()>0)
{
x = *b.begin(); b.erase(b.begin());
for(int i=0;i<g[x].size();i++)
{
y = g[x][i];
if( dst[y] > dst[x] + pr[x][y] && c[x][y] > f[x][y] )
{
dst[y] = dst[x] + pr[x][y];
tt[y] = x;
was[y] = 1;
if(y != d)v.insert(y);
}
}
}
swap(b,v);
}
return was[d];
}
void fmcm(){
int cost_min = 0, flux;
while( bfs() )
{
flux = Inf;
for(int x = d;x != s;x = tt[x]) flux = min(flux, c[tt[x]][x]-f[tt[x]][x]);
if( flux != 0)
{
for(int x = d; x!= s; x= tt[x])
{
f[tt[x]][x] += flux;
f[x][tt[x]] -= flux;
cost_min += flux * pr[tt[x]][x];
}
}
}
printf("%d\n",cost_min);
}
int main(){
int m,x,y,z,w;
freopen("fmcm.in","r",stdin);
freopen("fmcm.out","w",stdout);
scanf("%d %d %d %d",&n,&m,&s,&d);
while(m--)
{
scanf("%d %d %d %d",&x,&y,&z,&w);
g[x].push_back(y);
g[y].push_back(x);
c[x][y] = z;
pr[x][y] = w;
pr[y][x] = -w;
}
fmcm();
return 0;
}