Pagini recente » Cod sursa (job #2450125) | Cod sursa (job #2697889) | Cod sursa (job #56154) | Cod sursa (job #2150430) | Cod sursa (job #2769355)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define NMAX 355
using namespace std;
ifstream f("fmcm.in");
ofstream g("fmcm.out");
int n, m, dist[NMAX], flux, cost, t[NMAX], c[NMAX][NMAX], F[NMAX][NMAX], costuri[NMAX][NMAX];
int start, stop;
bitset <NMAX> v;
vector <pair <int, int>> edges[NMAX];
queue <int> q;
bool bf()
{
bool ok = 0;
for(int i = 1; i <= n; i++)
dist[i] = INF, v[i] = 0;
dist[start] = 0;
v[start] = 1;
q.push(start);
while(!q.empty())
{
int nod = q.front();
q.pop();
v[nod] = 0;
for(auto child : edges[nod])
if(c[nod][child.first] > F[nod][child.first] && dist[child.first] > dist[nod] + child.second)
{
dist[child.first] = dist[nod] + child.second;
t[child.first] = nod;
if(child.first == stop)
ok = 1;
if(!v[child.first])
{
q.push(child.first);
v[child.first] = 1;
}
}
}
return ok;
}
int main()
{
f >> n >> m >> start >> stop;
for(int i = 1; i <= m; i++)
{
int x, y, cost, cap;
f >> x >> y >> cap >> cost;
c[x][y] = cap;
costuri[x][y] = cost;
costuri[y][x] = -cost;
edges[x].push_back(make_pair(y, cost));
edges[y].push_back(make_pair(x, -cost));
}
while(bf())
{
int minim = INF;
int k = stop;
while(k != start)
{
minim = min(minim, c[t[k]][k] - F[t[k]][k]);
k = t[k];
}
k = stop;
while(k != start)
{
F[t[k]][k] += minim;
F[k][t[k]] -= minim;
cost += costuri[t[k]][k] * minim;
k = t[k];
}
}
g << cost;
return 0;
}