Guest User

Untitled

a guest
Jan 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1.  
  2. int N;
  3. int[][] G;
  4.  
  5. private int findFlow(int from, int to, int flow, boolean[] used) {
  6. if(from == to) {
  7. return flow;
  8. }
  9.  
  10. used[from] = true;
  11.  
  12. for(int u = 0; u < used.length; u++) {
  13. if(!used[u] && G[from][u] > 0) {
  14. int current_flow = findFlow(u, to, Math.min(flow, G[from][u]), used.clone());
  15.  
  16. if(current_flow > 0) {
  17. G[from][u] -= current_flow;
  18. G[u][from] += current_flow;
  19.  
  20. return current_flow;
  21. }
  22.  
  23. }
  24. }
  25.  
  26. return 0;
  27. }
  28.  
  29. private void solve() {
  30. N = in.readInt();
  31. int M = in.readInt();
  32.  
  33. G = new int[N][N];
  34.  
  35. int INF = Integer.MAX_VALUE / 2;
  36.  
  37. for(int i = 0; i < M; i++) {
  38. int from = in.readInt() - 1;
  39. int to = in.readInt() - 1;
  40. int cost = in.readInt();
  41.  
  42. G[from][to] = cost;
  43. }
  44.  
  45. int flow = 0;
  46.  
  47. while(true) {
  48. int current_flow = findFlow(0, N - 1, INF, new boolean[N]);
  49. flow += current_flow;
  50.  
  51. if(current_flow == 0) {
  52. break;
  53. }
  54. }
  55.  
  56. out.println(flow);
  57. }
Add Comment
Please, Sign In to add comment