Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int N;
- int[][] G;
- private int findFlow(int from, int to, int flow, boolean[] used) {
- if(from == to) {
- return flow;
- }
- used[from] = true;
- for(int u = 0; u < used.length; u++) {
- if(!used[u] && G[from][u] > 0) {
- int current_flow = findFlow(u, to, Math.min(flow, G[from][u]), used.clone());
- if(current_flow > 0) {
- G[from][u] -= current_flow;
- G[u][from] += current_flow;
- return current_flow;
- }
- }
- }
- return 0;
- }
- private void solve() {
- N = in.readInt();
- int M = in.readInt();
- G = new int[N][N];
- int INF = Integer.MAX_VALUE / 2;
- for(int i = 0; i < M; i++) {
- int from = in.readInt() - 1;
- int to = in.readInt() - 1;
- int cost = in.readInt();
- G[from][to] = cost;
- }
- int flow = 0;
- while(true) {
- int current_flow = findFlow(0, N - 1, INF, new boolean[N]);
- flow += current_flow;
- if(current_flow == 0) {
- break;
- }
- }
- out.println(flow);
- }
Add Comment
Please, Sign In to add comment