Pagini recente » Cod sursa (job #76239) | Cod sursa (job #1289701) | Cod sursa (job #2944670) | Cod sursa (job #2093532) | Cod sursa (job #1678421)
#include <iostream>
#include <fstream>
#include <queue>
#define oo 2147483647
using namespace std;
int n;
int c[1005][1005];
bool vis[1005];
int tata[1005];
int minCap(int in) {
int i = in;
int mn = c[i][n];
for(; tata[i]; i = tata[i]) {
mn = min(mn, c[tata[i]][i]);
}
if(mn > 0) {
c[in][n] -= mn;
c[n][in] += mn;
for(; tata[in]; in = tata[in]) {
c[tata[in]][in] -= mn;
c[in][tata[in]] += mn;
}
}
return mn;
}
int flow;
bool onPath() {
priority_queue<pair< int, int> > q;
q.push({oo, 1});
vis[1] = true;
while(!q.empty()) {
int cur = q.top().second;
int mn = q.top().first;
q.pop();
if(c[cur][n] > 0) {
//for(int i = 1; i <= n; i ++) vis[i] = false;
int capacity = minCap(cur);
if(capacity > 0) flow += capacity;
}
for(int i = 2; i < n; i ++) {
if(c[cur][i] > 0 && !vis[i]) {
vis[i] = true;
tata[i] = cur;
q.push({min(mn, c[cur][i]), i});
}
}
}
while(!q.empty()) q.pop();
bool ok = false;
for(int i = 1; i < n; i ++) {
if(vis[i] && c[i][n] > 0) ok = true;
vis[i] = false;
}
return ok;
}
void maxFlow() {
int capacity = 0;
while(true) {
if(!onPath()) break;
}
}
int main()
{
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int m, x, y;
f >> n >> m;
for(int i = 1; i <= m; i ++) {
f >> x >> y; f >> c[x][y];
}
maxFlow();
g << flow << "\n";
return 0;
}