Pagini recente » Cod sursa (job #1643963) | Cod sursa (job #2889716) | Cod sursa (job #976632) | Cod sursa (job #2812101) | Cod sursa (job #2462382)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("fmcm.in");
ofstream fout("fmcm.out");
const int DIM = 357;
const int INF = 1e9;
int n, m, s, d;
vector <int> v[DIM];
int cost[DIM][DIM];
int flux[DIM][DIM];
int cap[DIM][DIM];
int father[DIM];
int dist[DIM];
bool bellman()
{
queue <int> q;
for(int i = 1; i <= n; i++)
dist[i] = INF;
dist[s] = 0;
q.push(s);
bitset <DIM> vis;
vis[s] = true;
while(!q.empty())
{
int nod = q.front();
q.pop();
vis[nod] = false;
for(int i = 0; i < v[nod].size(); i++)
if(cap[nod][v[nod][i]] != 0)
{
if(dist[v[nod][i]] > dist[nod] + cost[nod][v[nod][i]])
{
father[v[nod][i]] = nod;
dist[v[nod][i]] = dist[nod] + cost[nod][v[nod][i]];
if(v[nod][i] != d && vis[v[nod][i]] == false)
{
vis[v[nod][i]] = true;
q.push(v[nod][i]);
}
}
}
}
if(dist[d] == INF)
return false;
return true;
}
main()
{
fin >> n >> m >> s >> d;
for(int i = 1; i <= m; i++)
{
int x, y, c, z;
fin >> x >> y >> c >> z;
cap[x][y] = c;
cost[x][y] = z;
cost[y][x] = -z;
v[x].push_back(y);
v[y].push_back(x);
}
int sum = 0;
while(bellman())
{
int flux = INF;
for(int i = d; i != s; i = father[i])
flux = min(flux, cap[father[i]][i]);
for(int i = d; i != s; i = father[i])
{
cap[father[i]][i] -= flux;
cap[i][father[i]] += flux;
sum += flux * cost[father[i]][i];
}
}
fout << sum;
}