Pagini recente » Cod sursa (job #2705903) | Cod sursa (job #976488) | Cod sursa (job #693070) | Cod sursa (job #630382) | Cod sursa (job #2824315)
#include <bits/stdc++.h>
using namespace std;
using pii = pair <int, int>;
inline void Open(const string Name) {
#ifndef ONLINE_JUDGE
(void)!freopen((Name + ".in").c_str(), "r", stdin);
(void)!freopen((Name + ".out").c_str(), "w", stdout);
#endif
}
priority_queue<pii, vector<pii>, greater <pii>> H;
queue <int> Q;
vector <int> con[351];
int C[351][351], Cst[351][351];
int d[351], real_d[351], p[351], old_d[351], inQ[351];
int N, M, S, D, F, FCst;
bool dijkstra() {
memset(d, 0x3f, sizeof(d));
d[S] = real_d[S] = 0;
H.emplace(d[S], S);
while(!H.empty()) {
int cst = H.top().first, nod = H.top().second;
H.pop();
if(d[nod] != cst)
continue;
for(int it : con[nod])
if(C[nod][it]) {
int new_d = d[nod] + Cst[nod][it] + old_d[nod] - old_d[it];
if(new_d < d[it]) {
d[it] = new_d;
real_d[it] = real_d[nod] + Cst[nod][it];
p[it] = nod;
H.emplace(d[it], it);
}
}
}
memcpy(old_d, real_d, sizeof(real_d));
if(d[D] == 0x3f3f3f3f)
return 0;
int Min = 0x3f3f3f3f, curCst = 0;
for(int aux = D;aux != S;aux = p[aux]) {
Min = min(Min, C[p[aux]][aux]);
curCst += Cst[p[aux]][aux];
}
F += Min;
FCst += Min * real_d[D];
for(int aux = D;aux != S;aux = p[aux]) {
C[p[aux]][aux] -= Min;
C[aux][p[aux]] += Min;
}
return 1;
}
void bellmanFord() {
memset(old_d, 0x3f, sizeof(old_d));
old_d[S] = 0;
for(Q.push(S), inQ[S] = 1;!Q.empty();Q.pop()) {
int i = Q.front();
inQ[i] = 0;
for(int it : con[i])
if(C[i][it]) {
if(old_d[i] + Cst[i][it] >= old_d[it])
continue;
old_d[it] = old_d[i] + Cst[i][it];
if(inQ[it])
continue;
inQ[i] = 1;
Q.emplace(it);
}
}
}
void solve() {
cin >> N >> M >> S >> D;
for(int i = 1, x, y;i <= M;i++) {
cin >> x >> y;
con[x].emplace_back(y);
con[y].emplace_back(x);
cin >> C[x][y] >> Cst[x][y];
Cst[y][x] = -Cst[x][y];
}
F = FCst = 0;
bellmanFord();
for(;dijkstra(););
cout << FCst;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Open("fmcm");
int T = 1;
for(;T;T--) {
solve();
}
return 0;
}