Pagini recente » Cod sursa (job #513280) | Cod sursa (job #1587111) | Cod sursa (job #2439758) | Cod sursa (job #2471309) | Cod sursa (job #2541598)
#include<fstream>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
struct Edge {
int to, c, revInd;
};
const int MAX_N = 1000;
const int INF = 110069;
vector<Edge> nbrs[MAX_N];
int prevInPath[MAX_N];
int pathMinC[MAX_N];
int edgeInd[MAX_N];
int n, m;
int s, t;
int findAndUpdatePath() {
queue<int> q;
q.push(s);
fill(prevInPath, prevInPath + n, -1);
fill(pathMinC, pathMinC + n, INF);
fill(edgeInd, edgeInd + n, -1);
prevInPath[s] = s;
while(!q.empty()) {
int curr = q.front();
if(curr == t) {
break;
}
q.pop();
for(int i = 0; i < nbrs[curr].size(); ++i) {
int next = nbrs[curr][i].to;
if(nbrs[curr][i].c == 0 || prevInPath[next] != -1) {
continue;
}
q.push(next);
prevInPath[next] = curr;
edgeInd[next] = i;
pathMinC[next] = min(pathMinC[curr], nbrs[curr][i].c);
}
}
if(prevInPath[t] == -1) {
return 0;
}
int currV = t;
while(currV != s) {
int prevV = prevInPath[currV];
int currEdgeInd = edgeInd[currV];
int twinEdgeInd = nbrs[prevV][currEdgeInd].revInd;
nbrs[prevV][currEdgeInd].c -= pathMinC[t];
nbrs[currV][twinEdgeInd].c += pathMinC[t];
currV = prevV;
}
return pathMinC[t];
}
int main() {
#define cout myfileOut
ofstream myfileOut;
myfileOut.open ("maxflow.out");
#define cin myFileIn
ifstream myFileIn;
myFileIn.open ("maxflow.in");
///ios_base::sync_with_stdio(false);
///cin.tie(0);
cin >> n >> m;
/// cin >> s >> t;
--s;
--t;
/// idiot
s = 0;
t = n - 1;
for(int i = 0; i < m; ++i) {
int from, to, c;
cin >> from >> to >> c;
--from;
--to;
int frontRevInd = nbrs[to].size();
int backRevInd = nbrs[from].size();
nbrs[from].push_back({to, c, frontRevInd});
nbrs[to].push_back({from, 0, backRevInd});
}
int res = 0;
while(true) {
int flowUpdate = findAndUpdatePath();
res += flowUpdate;
if(flowUpdate == 0) {
break;
}
}
cout << res << endl;
return 0;
}