Pagini recente » Cod sursa (job #2285783) | Cod sursa (job #183130) | Cod sursa (job #2114150) | Cod sursa (job #1911872) | Cod sursa (job #2230928)
#include <bits/stdc++.h>
using namespace std;
const int N = 360;
int n, m, t, s, cp[N][N], cost[N][N], d[N], d2[N], aux[N], pr[N], ans;
vector <int> v[N];
queue <int> Q;
bool u[N];
set <pair<int,int> > S;
void bellman(){
for (int i=1; i<=n; i++) d[i] = 1e9;
d[s] = 0;
Q.push(s);
while (!Q.empty()){
int nod = Q.front();
Q.pop();
u[nod] = 0;
for(auto it: v[nod]){
if (!cp[nod][it]) continue;
if (d[nod] + cost[nod][it] < d[it]){
d[it] = d[nod] + cost[nod][it];
if (!u[it]) u[it] = 1, Q.push(it);
}
}
}
}
bool dijkstra(){
for (int i=1; i<=n; i++) d2[i] = 1e9, pr[i] = 0;
d2[s] = aux[s] = 0;
S.insert({0, s});
while (S.size()){
int nod = S.begin()->second, costcr = S.begin()->first;
S.erase(S.begin());
if (costcr != d2[nod] || nod == t) continue;
for (auto it: v[nod]){
if (!cp[nod][it]) continue;
if (d2[nod] + cost[nod][it] < d2[it]){
d2[it] = d2[nod] + cost[nod][it];
pr[it] = nod;
S.insert({d2[it], it});
aux[it] = aux[nod] + cost[nod][it];
}
}
}
return pr[t];
}
int main(){
ifstream cin ("fmcm.in");
ofstream cout ("fmcm.out");
cin >> n >> m >> s >> t;
for (int i=1, x, y, z, c; i<=m; i++){
cin >> x >> y >> z >> c;
v[x].push_back(y);
cost[x][y] = c;
cp[x][y] = z;
v[y].push_back(x);
cost[y][x] = -c;
}
bellman();
while (dijkstra()){
for (int i=1; i<=n; i++) d[i] = aux[i];
int flux = 1e9;
for (int nod = t; nod != s; nod = pr[nod])
flux = min(flux, cp[pr[nod]][nod]);
ans += d[t] * flux;
for (int nod = t; nod != s; nod = pr[nod]){
cp[pr[nod]][nod] -= flux;
cp[nod][pr[nod]] += flux;
}
}
cout << ans;
return 0;
}