Pagini recente » Cod sursa (job #657075) | Cod sursa (job #612917) | Cod sursa (job #771418) | Cod sursa (job #1791710) | Cod sursa (job #2457375)
#include <bits/stdc++.h>
using namespace std;
ifstream f("fmcm.in");
ofstream g("fmcm.out");
const int N = 360;
const int oo = 1000000000;
int n, m, flux, Flux, s, d, x, y, c, z, fl[N][N], cap[N][N], cst[N], T[N];
vector <pair<int, int>> v[N];
bitset <N> Q;
queue <int> q;
int Bellman()
{
T[s] = s;
for(int i = 1; i <= n; i++)
cst[i] = oo;
cst[s] = 0;
Q[s] = 1;
q.push(s);
while(!q.empty())
{
int nod = q.front();
for(auto it:v[nod])
{
int vec, C;
tie(vec, C) = it;
if(cap[nod][vec] > fl[nod][vec])
{
if(cst[vec] > cst[nod] + C)
{
cst[vec] = cst[nod] + C;
if(!Q[vec])
{
q.push(vec);
Q[vec] = 1;
}
T[vec] = nod;
}
}
}
q.pop();
Q[nod] = 0;
}
if(cst[d] == oo)
return 0;
return 1;
}
void update()
{
for(x=d,y = T[d], flux = oo; x != s; x = T[x], y = T[y])
flux = min(flux, cap[y][x] - fl[y][x]);
for(x=d,y = T[d]; x != s; x = T[x], y = T[y])
fl[y][x] += flux, fl[x][y] -= flux;
Flux += flux * cst[d];
}
int main()
{
f >> n >> m >> s >> d;
for(; m; m--)
{
f >> x >> y >> c >> z;
v[x].push_back(make_pair(y, z));
v[y].push_back(make_pair(x, -z));
cap[x][y] = c;
}
while(Bellman())
update();
g << Flux;
return 0;
}