Pagini recente » Istoria paginii runda/oni_2016_cl10_ziua2 | Cod sursa (job #3239617) | Cod sursa (job #1370290) | Sandbox (cutiuţa cu năsip) | Cod sursa (job #1865531)
#include <fstream>
#include <queue>
#define ll long long
using namespace std;
ifstream in("fmcm.in");
ofstream out("fmcm.out");
vector <int> ls[355];
ll cap[355][355], f[355][355], cs[355][355], dist[355], fmin, sol;
const ll f_mare = 2e11;
int n, m, inc, sf, x, y, z, t, tt[355], g[355];
queue <int> q;
bool viz[355], ok;
ll bf() {
int i, l;
for (i = 1; i <= n; i++) {
dist[i] = f_mare;
tt[i] = -1;
}
dist[inc] = 0;
q.push(inc);
viz[inc] = 1;
while (q.empty() == 0) {
x = q.front();
q.pop();
viz[x]=0;
for (i = 0; i < g[x]; i++) {
y = ls[x][i];
if (dist[y] > dist[x] + cs[x][y] && cap[x][y]-f[x][y] > 0) {
if (!viz[y]) {q.push(y); viz[y] = 1;}
dist[y] = dist[x] + cs[x][y];
tt[y] = x;
}
}
}
if (dist[y] < f_mare) {
ok = 1;
fmin = f_mare;
y = sf;
while (y != inc) {
fmin = min(fmin, cap[tt[y]][y] - f[tt[y]][y]);
y = tt[y];
}
y = sf;
while (y != inc) {
f[tt[y]][y] += fmin;
f[y][tt[y]] -= fmin;
y = tt[y];
}
return 1LL*dist[sf]*fmin;
}
return 0;
}
void calc() {
ok=1;
while (ok) {
ok = 0;
sol += bf();
}
}
int main() {
in >> n >> m >> inc >> sf;
while (m--) {
in >> x >> y >> z >> t;
ls[x].push_back(y);
ls[y].push_back(x);
cap[x][y] = z;
cs[x][y] = t;
cs[y][x] = -t;
}
for (int i = 1; i <= n; i++)
g[i] = ls[i].size();
calc();
out << sol;
return 0;
}