Pagini recente » Cod sursa (job #3215618) | Planificare infoarena | Cod sursa (job #1621132) | Planificare infoarena | Cod sursa (job #1892225)
#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
FILE *fin = freopen("fmcm.in", "r", stdin);
FILE *fout = freopen("fmcm.out", "w", stdout);
const int MAX_N = 355;
const int INF = 2000000000;
int N, M, S, D;
int ans;
int dist[MAX_N], fromWhere[MAX_N], flow[MAX_N][MAX_N];
int cost[MAX_N][MAX_N], carry[MAX_N][MAX_N];
bool used[MAX_N];
vector < int > v[MAX_N];
queue < int > q;
int BellmanFord()
{
for (int i = 1; i <= N; i++)
dist[i] = INF,
fromWhere[i] = -1;
memset(used, 0, sizeof(used));
dist[S] = 0;
used[S]= 1;
q.push(S);
while (!q.empty())
{
int p = q.front();
q.pop();
vector < int > :: iterator it;
for (it = v[p].begin(); it != v[p].end(); it++)
if (carry[p][*it] - flow[p][*it] && dist[*it] > dist[p] + cost[p][*it])
{
dist[*it] = dist[p] + cost[p][*it];
fromWhere[*it] = p;
if (!used[*it])
{
used[*it] = 1;
q.push(*it);
}
}
}
if (dist[D] != INF)
{
int cf = INF;
for (int i = D; i != S; i = fromWhere[i])
cf = min(cf, carry[fromWhere[i]][i] - flow[fromWhere[i]][i]);
for (int i = D; i != S; i = fromWhere[i])
{
flow[fromWhere[i]][i] += cf;
flow[i][fromWhere[i]] -= cf;
}
ans += cf * dist[D];
return 1;
}
return 0;
}
int main()
{
scanf("%d%d%d%d", &N, &M, &S, &D);
for (int i = 1, A, B, C, Z; i <= M; i ++)
{
scanf("%d%d%d%d", &A, &B, &C, &Z);
v[A].push_back(B);
v[B].push_back(A);
cost[A][B] = Z;
cost[B][A] = -Z;
carry[A][B] = C;
}
while(BellmanFord());
printf("%d\n", ans);
}