Pagini recente » Cod sursa (job #2413918) | Cod sursa (job #1297370) | Cod sursa (job #1881888) | Cod sursa (job #1098445) | Cod sursa (job #2806679)
#include <bits/stdc++.h>
#define inf 2e9
using namespace std;
ifstream fin("fmcm.in");
ofstream fout("fmcm.out");
const int nmax = 355;
int n, m, s, d;
int cap[nmax][nmax], cost[nmax][nmax], f[nmax][nmax];
int cst[nmax], tt[nmax];
bool ok, vis[nmax];
vector <int> v[nmax];
void read(){
fin >> n >> m >> s >> d;
for(int i = 1; i <= m; i++){
int x, y, c, z;
fin >> x >> y >> c >> z;
v[x].push_back(y);
v[y].push_back(x);
cap[x][y] = c;
cost[x][y] = z;
cost[y][x] = -z;
}
}
int bellmanFord(){
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; i++){
cst[i] = inf;
tt[i] = -1;
}
vector <int> q;
q.push_back(s);
cst[s] = 0;
vis[s] = 1;
for(int i = 0; i < q.size(); i++){
int x = q[i];
for(int j = 0; j < v[x].size(); j++){
int y = v[x][j];
if(cap[x][y] - f[x][y] > 0 && cst[x] + cost[x][y] < cst[y]){
cst[y] = cst[x] + cost[x][y];
tt[y] = x;
if(!vis[y]){
q.push_back(y);
vis[y] = 1;
}
}
}
vis[x] = 0;
}
if(cst[d] != inf){
int fluxMin = inf;
ok = 1;
for(int i = d; i != s; i = tt[i])
fluxMin= min(fluxMin, cap[tt[i]][i] - f[tt[i]][i]);
for(int i = d; i != s; i = tt[i]){
f[tt[i]][i] += fluxMin;
f[i][tt[i]] -= fluxMin;
}
return fluxMin * cst[d];
}
return 0;
}
void solve(){
int cost_min = 0;
ok = 1;
while(ok){
ok = 0;
cost_min += bellmanFord();
}
fout << cost_min;
}
int main()
{
read();
solve();
return 0;
}