-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmincut.cpp
More file actions
111 lines (95 loc) · 2.39 KB
/
mincut.cpp
File metadata and controls
111 lines (95 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define V 20
int bfs(int graph[][V],int s,int t,int parent[],int n){
bool visited[n];
for(int i=0;i<n;i++)
visited[i]=false;
visited[s] = true;
parent[s] = -1;
queue<int> q ;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
for(int i=0;i<n;i++){
if(!visited[i] && graph[u][i]){
visited[i] = true;
parent[i] = u;
q.push(i);
}
}
}
if(visited[t]) return 1;
else return 0;
}
void dfs(int graph[][V],int s, bool visited[],int n){
visited[s]=true;
for(int i=0;i<n;i++){
if(graph[s][i]>0 && !visited[i])
dfs(graph,i,visited,n);
}
}
void mincut(int graph[][V],int s,int t,int n){
int temp[n][V];
bool visited[n];
int parent[n];
for(int i =0;i<n;i++){
visited[i] =false;
parent[i] = -1;
for(int j=0;j<n;j++){
temp[i][j] = graph[i][j];
}
}
while(bfs(temp,s,t,parent,n)){
int minflow = INT_MAX;
for(int i = t;i!=s;i=parent[i]){
minflow = min(minflow,temp[parent[i]][t]);
}
for(int i = t;i!=s;i=parent[i]){
temp[parent[i]][i] -= minflow;
}
}
for(int i =0;i<n;i++){
visited[i] =false;
}
dfs(temp,s,visited,n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(visited[i]&&!visited[j])
cout<<i<<" - "<<j<<endl;
}
}
}
int main(){
int n;
cin>>n;
int graph[n][V];
for(int i =0;i<n;i++){
for(int j=0;j<n;j++){
graph[i][j]=0;
}
}
int a = 1;
int x,y,w;
while(a==1){
cout<<"Enetr vertexes : ";
cin>>x>>y;
cout<<"Enter weight :";
cin>>w;
graph[x-1][y-1] = w;
cout<<"Add more? 1/0 ";
cin>>a;
}
cout<<"Enter s and t ";
int s,t;
cin>>s>>t;
mincut(graph,s,t,n);
return 0;
}