Pagini recente » Cod sursa (job #3031721) | Cod sursa (job #2753036)
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
#include <climits>
using namespace std;
ifstream be("maxflow.in");
ofstream ki("maxflow.out");
void beolvas(vector<vector<int>>& adj, int m);
bool bfs(const vector<vector<int>>& adj, int n, int s, int t, vector<int> &ut);
void maxfolyam(vector<vector<int>> &adj, int n, int s, int t);
int main(){
int n, m, s, t;
be >> n >> m /*>> s >> t*/;
//s--;
//t--;
vector<vector<int>> adj(n, vector<int>(n, 0));
beolvas(adj, m);
maxfolyam(adj, n, 0, n-1);
}
void beolvas(vector<vector<int>>& adj, int m){
for (int i = 0; i < m; i++) {
int x, y, z;
be >> x >> y >> z;
adj[x - 1][y - 1] = z;
}
}
bool bfs(const vector<vector<int>>& adj, int n, int s, int t, vector<int>& ut) {
vector<bool> latogatott(n, false);
queue<int> q;
q.push(s);
latogatott[s] = true;
ut[s] = -1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v = 0; v < n; v++) {
if (adj[u][v] != 0 && !latogatott[v]) {
q.push(v);
ut[v] = u;
latogatott[v] = true;
}
}
}
return latogatott[t] == true;
}
void maxfolyam(vector<vector<int>>& adj, int n, int s, int t) {
vector<int> ut(n,-1);
int max_folyam = 0;
while (bfs(adj, n, s, t, ut)) {
int folyam = INT_MAX;
for (int v = t; v != s; v = ut[v]){
int u = ut[v];
folyam = min(folyam, adj[u][v]);
}
for (int v = t; v != s; v = ut[v]){
int u = ut[v];
adj[u][v] -= folyam;
adj[v][u] += folyam;
}
max_folyam += folyam;
}
ki << max_folyam << endl;
}