Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1.  
  2. import java.util.*;
  3.  
  4. public class Maksimivirtaus {
  5.  
  6. public int n;
  7. public int[][] verkko;
  8. public int[][] alussa;
  9. public int[] tila;
  10. public boolean loytyi;
  11. ArrayDeque<Integer> polku;
  12.  
  13. public Maksimivirtaus(int n) {
  14. this.n = n;
  15. tila = new int[n + 1];
  16. polku = new ArrayDeque<>();
  17. loytyi = false;
  18. verkko = new int[n + 1][n + 1];
  19. alussa = new int[n + 1][n + 1];
  20. }
  21.  
  22. public void lisaaKaari(int a, int b, int p) {
  23. verkko[a][b] += p;
  24. alussa[a][b] += p;
  25. }
  26.  
  27. void syvyyshaku(int s) {
  28. if (tila[s] == 1) {
  29. return;
  30. }
  31. if (loytyi) {
  32. return;
  33. }
  34. polku.addLast(s);
  35. if (s == n) {
  36. System.out.println("polku löytyi :)");
  37. loytyi = true;
  38. return;
  39. }
  40. tila[s] = 1;
  41. for (int i = 1; i <= n; i++) {
  42. if (verkko[s][i] > 0) {
  43. syvyyshaku(i);
  44. }
  45. }
  46. if (loytyi) {
  47. return;
  48. }
  49. polku.removeLast();
  50. }
  51.  
  52. public int laske() {
  53. int pienin = 999999999;
  54. int virtaus = 0;
  55.  
  56. for (int a = 1;;) {
  57.  
  58. syvyyshaku(a);
  59. if (loytyi) {
  60. System.out.println(polku);
  61. int edellinen = 0;
  62. for (Integer nykyinen : polku) {
  63. if (edellinen != 0) {
  64. pienin = Math.min(pienin, verkko[edellinen][nykyinen]);
  65. }
  66. edellinen = nykyinen;
  67. }
  68. System.out.println("Pienin: " + pienin);
  69. edellinen = 0;
  70.  
  71. for (Integer nykyinen : polku) {
  72. if (edellinen != 0) {
  73. verkko[edellinen][nykyinen] -= pienin;
  74. verkko[nykyinen][edellinen] += pienin;
  75. }
  76. edellinen = nykyinen;
  77. }
  78. for (int i = 1; i <= n; i++) {
  79. for (int j = 1; j <= n; j++) {
  80. if (alussa[i][j] > 0 && alussa[i][j] != verkko[i][j]) {
  81. System.out.println(i + " " + j + " " + (alussa[i][j] - verkko[i][j]));
  82. }
  83. }
  84. }
  85. }
  86.  
  87. virtaus += pienin;
  88.  
  89. return virtaus;
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement