Pagini recente » Cod sursa (job #2309132) | Cod sursa (job #2503843) | Cod sursa (job #598617) | Cod sursa (job #488590) | Cod sursa (job #2479198)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("fmcm.in");
ofstream fout("fmcm.out");
int n, m, s, d;
vector<int> G[351];
int C[351][351], F[351][351], cost[351][351], tata[351], dist[351], cntInPQ[351];
bool inPQ[351];
struct cmp{
bool operator () (int a, int b)
{
return dist[a] > dist[b];
}
};
bool dijkstra()
{
fill(dist + 1, dist + n + 1, 2000000000);
fill(cntInPQ + 1, cntInPQ + n + 1, 0);
priority_queue<int, vector<int>, cmp> pq;
dist[s] = 0;
pq.push(s);
while(!pq.empty())
{
int node = pq.top();
pq.pop();
++cntInPQ[node];
inPQ[node] = false;
if(cntInPQ[node] > n) return false;
if(node == d) continue;
for(int next : G[node])
{
if(C[node][next] == F[node][next]) continue;
if(dist[node] + cost[node][next] < dist[next])
{
dist[next] = dist[node] + cost[node][next];
tata[next] = node;
if(!inPQ[next])
{
inPQ[next] = true;
pq.push(next);
}
}
}
}
if(dist[d] == 2000000000) return false;
return true;
}
int main()
{
fin >> n >> m >> s >> d;
int x, y, c, w;
for(int i = 1; i <= m; ++i)
{
fin >> x >> y >> c >> w;
G[x].push_back(y);
G[y].push_back(x);
C[x][y] = c;
cost[x][y] = w;
cost[y][x] = -w;
}
int mincost = 0;
while(dijkstra())
{
int flow = 2000000000;
for(int i = d; i != s; i = tata[i])
flow = min(flow, C[tata[i]][i] - F[tata[i]][i]);
for(int i = d; i != s; i = tata[i])
{
mincost = mincost + flow*cost[tata[i]][i];
F[tata[i]][i] += flow;
F[i][tata[i]] -= flow;
}
}
fout << mincost;
}