Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define MAXN 1005
- #define MAXM 5005
- #define INF 0x3f3f3f3f
- using namespace std;
- int n, m, C[MAXN][MAXN], parent[MAXN];
- int viz[MAXN];
- vector<int> G[MAXN];
- int bfs()
- {
- memset(viz, 0, (n + 1) * sizeof(int));
- memset(parent, 0, (n + 1) * sizeof(int));
- queue<int> q;
- viz[1] = 1;
- q.push(1);
- while(!q.empty()) {
- int nod = q.front();
- q.pop();
- if(nod == n)
- continue;
- for(int i : G[nod]) {
- if(!viz[i] && C[nod][i] > 0) {
- viz[i] = 1;
- parent[i] = nod;
- q.push(i);
- }
- }
- }
- return viz[n];
- }
- int main()
- {
- ifstream in("maxflow.in");
- ofstream out("maxflow.out");
- in >> n >> m;
- vector<int> vec_in;
- while(m--) {
- int x, y, z;
- in >> x >> y >> z;
- C[x][y] = z;
- G[x].push_back(y);
- G[y].push_back(x);
- }
- int tflow = 0;
- while(bfs()) {
- for(int j: G[n]) {
- if(C[j][n] > 0 && viz[j]) {
- parent[n] = j;
- int flow = INF;
- for(int i = n; i != 1; i = parent[i]) {
- if(C[parent[i]][i] < flow) {
- flow = C[parent[i]][i];
- }
- }
- tflow += flow;
- for(int i = n; i != 1; i = parent[i]) {
- C[parent[i]][i] -= flow;
- C[i][parent[i]] += flow;
- }
- }
- }
- }
- out << tflow << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement