Pagini recente » Cod sursa (job #2773778) | Cod sursa (job #2874978) | Cod sursa (job #1645088) | Cod sursa (job #2664127) | Cod sursa (job #2372345)
#include <bits/stdc++.h>
using namespace std;
int n, m, s, d;
vector<vector<int> >graph, capacity, flow, cost;
vector<int> parent;
vector<int> dist;
struct cmp{
bool operator()(int a, int b){
return dist.at(a) > dist.at(b);
}
};
priority_queue<int, vector<int>, cmp> pQueue;
bool dijkstra(){
dist.clear();
dist.resize(n+1, INT_MAX);
parent.clear();
parent.resize(n+1);
vector<bool> inQueue = vector<bool>(n+1, false);
dist.at(s) = 0;
pQueue.push(s);
while(!pQueue.empty()){
int node = pQueue.top();
pQueue.pop();
inQueue.at(node) = false;
for(auto& neighbour:graph.at(node)){
if(capacity.at(node).at(neighbour)==flow.at(node).at(neighbour)) continue;
if(dist.at(node)+cost.at(node).at(neighbour) < dist.at(neighbour)){
dist.at(neighbour) = dist.at(node)+cost.at(node).at(neighbour);
parent.at(neighbour) = node;
if(!inQueue.at(neighbour)){
inQueue.at(neighbour) = true;
pQueue.push(neighbour);
}
}
}
}
if(dist.at(d)!=INT_MAX) return true;
return false;
}
int main()
{
ifstream fin("fmcm.in");
ofstream fout("fmcm.out");
fin>>n>>m>>s>>d;
int x, y, z, c;
graph.resize(n+1, vector<int>());
capacity.resize(n+1, vector<int>(n+1));
flow.resize(n+1, vector<int>(n+1));
cost.resize(n+1, vector<int>(n+1));
for(int i=1; i<=m; i++){
fin>>x>>y>>z>>c;
graph.at(x).push_back(y);
graph.at(y).push_back(x);
cost.at(x).at(y) = c;
cost.at(y).at(x) = - c;
capacity.at(x).at(y) = z;
}
int cmin = 0, cflow;
while(dijkstra()){
cflow = INT_MAX;
for(int i=d; i!=s; i = parent.at(i))
cflow = min(cflow, capacity.at(parent.at(i)).at(i) - flow.at(parent.at(i)).at(i));
for(int i=d; i!=s; i = parent.at(i)){
flow.at(parent.at(i)).at(i) += cflow;
flow.at(i).at(parent.at(i)) -= cflow;
cmin += cost.at(parent.at(i)).at(i)*cflow;
}
}
fout<<cmin;
}