Pagini recente » Cod sursa (job #238308) | Cod sursa (job #3164929) | Cod sursa (job #3170726) | Cod sursa (job #163257) | Cod sursa (job #2371953)
#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);
vector<bool> inQueue = vector<bool>(n+1, false);
dist.at(s) = 0;
pQueue.push(s);
vector<bool> visited = vector<bool>(n+1, false);
while(!pQueue.empty()){
int node = pQueue.top();
pQueue.pop();
inQueue.at(node) = false;
visited.at(node) = true;
for(auto& neighbour:graph.at(node)){
if(capacity.at(node).at(neighbour)==flow.at(node).at(neighbour) || visited.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) = cost.at(y).at(x) = c;
capacity.at(x).at(y) = z;
}
parent.resize(n+1);
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;
}