Pagini recente » Cod sursa (job #2875141) | Cod sursa (job #40648) | Cod sursa (job #694541) | Cod sursa (job #2368211) | Cod sursa (job #2839428)
#include <bits/stdc++.h>
//#pragma GCC optimize ("03")
#define FastIO ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define FILES freopen("fmcm.in" , "r" , stdin) , freopen("fmcm.out" , "w" , stdout)
#define ll long long
#define ull unsigned long long
#define ld long double
#define eb emplace_back
#define pb push_back
#define qwerty1 first
#define qwerty2 second
#define qwerty3 -> first
#define qwerty4 -> second
#define umap unordered_map
#define uset unordered_set
#define pii pair < int , short >
namespace FastRead
{
char __buff[5000];short __lg = 0 , __p = 0;
char nc()
{
if(__lg == __p){__lg = fread(__buff , 1 , 5000 , stdin);__p = 0;if(!__lg) return EOF;}
return __buff[__p++];
}
template<class T>void read(T&__x)
{
T __sgn = 1; char __c;while(!isdigit(__c = nc()))if(__c == '-')__sgn = -1;
__x = __c - '0';while(isdigit(__c = nc()))__x = __x * 10 + __c - '0';__x *= __sgn;
}
}
using namespace FastRead;
using namespace std;
const short N = 355;
short n , m , x , y , c , z , sink , source;
vector < short > G[N];
int cost[N][N], dist[N] , d[N];
short parent[N] , capacity[N][N];
bool inq[N];
queue < short > q;
pii pq[N];
short pos[N];
short cnt = 0;
void HeapUp(short p)
{
while(p > 1 && pq[p] < pq[p / 2])
{
swap(pos[pq[p].second] , pos[pq[p / 2].second]);
swap(pq[p] , pq[p / 2]);
p /= 2;
}
}
void HeapDown(short p)
{
bool ok = 0;
do
{
ok = 0;
short sw = 0;
if(p * 2 <= cnt)
sw = p * 2;
if(p * 2 + 1 <= cnt)
if(pq[p * 2] > pq[p * 2 + 1])
sw = p * 2 + 1;
if(sw && pq[p] > pq[sw])
{
swap(pos[pq[p].second] , pos[pq[sw].second]);
swap(pq[p] , pq[sw]);
p = sw;
ok = 1;
}
}while(ok);
}
void insert(pii x)
{
if(pos[x.second])
{
pq[pos[x.second]] = x;
HeapDown(pos[x.second]);
HeapUp(pos[x.second]);
}
else
{
pq[++cnt] = x;
pos[x.second] = cnt;
HeapUp(cnt);
}
}
void pop()
{
pos[pq[1].second] = 0;
if(cnt > 1)
{
pq[1] = pq[cnt];
pos[pq[1].second] = 1;
--cnt;
HeapDown(1);
}
else cnt--;
}
void inline bellman()
{
for(short i = 1 ; i <= n ; i++)
dist[i] = INT_MAX;
dist[source] = 0;
q.push(source);
inq[source] = 1;
while(!q.empty())
{
short node = q.front(); q.pop();
inq[node] = 0;
for(int j = 0 ; j < G[node].size() ; j++)
{
int i = G[node][j];
if(capacity[node][i] && dist[node] + cost[node][i] < dist[i])
{
dist[i] = dist[node] + cost[node][i];
if(!inq[i]) q.push(i);
inq[i] = 1;
}
}
}
}
bool inline djikstra()
{
for(short i = 1 ; i <= n ; i++)
d[i] = INT_MAX , parent[i] = 0;
d[source] = 0;
insert({0 , source});
while(cnt)
{
short node = pq[1].second;
int curr_dist = pq[1].first;
pop();
if(d[node] != curr_dist) continue;
for(short j = 0 ; j < G[node].size() ; j++)
{
short to = G[node][j];
if(capacity[node][to] && d[node] + (dist[node] + cost[node][to] - dist[to]) < d[to])
{
d[to] = d[node] + (dist[node] + cost[node][to] - dist[to]);
parent[to] = node;
insert({d[to] , to});
}
}
}
return d[sink] != INT_MAX;
}
int inline Flow()
{
int flow = 0;
int mnCost = 0;
while(djikstra())
{
int mnFlow = INT_MAX;
for(short curr = sink ; curr != source ; curr = parent[curr])
mnFlow = min(mnFlow , (int) capacity[parent[curr]][curr]);
flow += mnFlow;
for(short curr = sink ; curr != source ; curr = parent[curr])
{
capacity[parent[curr]][curr] -= mnFlow;
capacity[curr][parent[curr]] += mnFlow;
mnCost += mnFlow * cost[parent[curr]][curr];
}
}
return mnCost;
}
int main()
{
ifstream f ("fmcm.in");
ofstream g ("fmcm.out");
// read(n) , read(m) , read(source) , read(sink);
f >> n >> m >> source >> sink;
while(m--)
{
f >> x >> y >> c >> z;
//read(x) , read(y) , read(c) , read(z);
G[x].pb(y);
G[y].pb(x);
capacity[x][y] = c;
capacity[y][x] = 0;
cost[x][y] = z;
cost[y][x] = -z;
}
bellman();
g << Flow();
return 0;
}