Pagini recente » Cod sursa (job #1509284) | Cod sursa (job #2642338) | Cod sursa (job #835007) | Cod sursa (job #3157075) | Cod sursa (job #2117166)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 355;
const int INF = (1<<30);
int capacity[MAXN][MAXN];
int current_flow[MAXN][MAXN];
int dist[MAXN];
vector< pair<int, int> > gr[MAXN];
int boss[MAXN];
ifstream f("fmcm.in");
ofstream g("fmcm.out");
class cmp{
public:
const bool operator()(const pair<int, int> &a, const pair<int, int> & b) const{
return a.second > b.second;
}
};
priority_queue< pair<int, int>, vector< pair<int, int> >, cmp > H;
bool dijkstra(int source, int target, int n){
for(int i = 1; i <= n; ++i){
dist[i] = INF;
}
memset(boss, 0, sizeof boss);
dist[source] = 0;
H.push({source, 0});
while(H.size()){
int node = H.top().first;
int cost = H.top().second;
H.pop();
if(cost != dist[node]) continue;
for(auto x : gr[node]){
if(cost + x.second < dist[x.first] && current_flow[node][x.first] < capacity[node][x.first]){
dist[x.first] = cost + x.second;
boss[x.first] = node;
H.push({x.first, dist[x.first]});
}
}
}
return dist[target] != INF;
}
int main(){
int n, m, source, target;
f >> n >> m >> source >> target;
while(m--){
int a, b, c, d;
f >> a >> b >> c >> d;
gr[a].push_back({b, d});
capacity[a][b] = c;
}
int ans = 0;
while(dijkstra(source, target, n)){
int node = target;
int minimum_flow = INF;
while(node != source){
minimum_flow = min(minimum_flow, capacity[ boss[node] ][node] - current_flow[ boss[node] ][node]);
node = boss[node];
}
node = target;
while(node != source){
current_flow[ boss[node] ][node] += minimum_flow;
current_flow[node][ boss[node] ] -= minimum_flow;
node = boss[node];
}
ans += (dist[target] * minimum_flow);
}
g << ans;
return 0;
}