Pagini recente » Cod sursa (job #80543) | Cod sursa (job #796850) | Cod sursa (job #1292799) | Cod sursa (job #1877564) | Cod sursa (job #1292787)
#include<cstdio>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<bitset>
#include<deque>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<unordered_map>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
using namespace std;
const int nmax = 355;
const int inf = (1LL << 31) - 1;
int i, n, m, s, d, x, y, z, t, ctotal, add;
int cap[nmax][nmax], cost[nmax][nmax];
int flow[nmax][nmax], dist[nmax], f[nmax];
vector<int> v[nmax];
deque<int> q;
bitset<nmax> inq;
bool bf()
{
for(i = 1; i <= n; i++)
dist[i] = inf;
dist[s] = 0;
f[s] = s;
inq[s] = 1;
q.push_back(s);
while(!q.empty())
{
x = q.front();
q.pop_front();
inq[x] = 0;
for(auto it : v[x])
if(dist[x] + cost[x][it] < dist[it] && flow[x][it] < cap[x][it])
{
dist[it] = dist[x] + cost[x][it];
f[it] = x;
if(!inq[it])
{
inq[it] = 1;
q.push_back(it);
}
}
}
return dist[d] != inf;
}
int main()
{
freopen("fmcm.in", "r", stdin);
freopen("fmcm.out", "w", stdout);
scanf("%d%d%d%d", &n, &m, &s, &d);
for(; m; m--)
{
scanf("%d%d%d%d", &x, &y, &z, &t);
v[x].push_back(y);
v[y].push_back(x);
cap[x][y] = z;
cost[x][y] = t;
cost[y][x] = -t;
}
while(bf())
{
add = inf;
for(x = d; x != f[x]; x = f[x])
add = min(add, cap[f[x]][x] - flow[f[x]][x]);
for(x = d; x != f[x]; x = f[x])
{
flow[f[x]][x] += add;
flow[x][f[x]] -= add;
}
ctotal += dist[d] * add;
}
printf("%d\n", ctotal);
return 0;
}