Advertisement
Guest User

ditch

a guest
Jan 16th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. /*
  2. ID: kh787591
  3. LANG: JAVA
  4. TASK: ditch
  5. */
  6.  
  7. import java.io.*;
  8. import java.util.*;
  9.  
  10. class ditch
  11. {
  12.  
  13. static final int MAXN = 800 + 5;
  14. static final int MAXM = 2900 + 5;
  15. final static int INF = 100000008;
  16. public static void main(String...args)throws Exception{
  17.  
  18. BufferedReader bf = new BufferedReader(new FileReader("ditch.in"));
  19. PrintWriter pw = new PrintWriter(new File("ditch.out"));
  20.  
  21. StringTokenizer st = new StringTokenizer(bf.readLine());
  22.  
  23. int ditches = Integer.parseInt(st.nextToken());
  24. int sink = Integer.parseInt(st.nextToken());
  25. init(sink);
  26.  
  27. for(int i = 0; i < ditches; ++i) {
  28. st = new StringTokenizer(bf.readLine());
  29. int r = Integer.parseInt(st.nextToken());
  30. int c = Integer.parseInt(st.nextToken());
  31. adde(r,c, Integer.parseInt(st.nextToken()));
  32. adde(c, r, 0);
  33. }
  34. int totalFlow = 0;
  35.  
  36. PriorityQueue<point> pQ = new PriorityQueue<>();
  37. boolean [] inQ = new boolean[sink+3];
  38. int[] dist = new int[sink+3];
  39. int[] father = new int[sink+3];
  40. System.out.println(1);
  41. while(dist[sink] != INF){
  42.  
  43. for(int i = 0; i <= sink; ++i){
  44. dist[i] = INF;
  45. father[i] = -1;
  46. }
  47.  
  48. pQ = new PriorityQueue<>();
  49. inQ = new boolean[sink+3];
  50. dist[1] = 0;
  51. pQ.add(new point(1, 0));
  52. inQ[1] = true;
  53. int smallest = INF;
  54.  
  55. while(!pQ.isEmpty()){
  56.  
  57. point cur = pQ.remove();
  58. inQ[cur.ID] = false;
  59. if(cur.dist != dist[cur.ID]){
  60. continue;
  61. }
  62. if(cur.ID == sink){
  63. break;
  64. }
  65. for (int k = last[cur.ID]; k != -1; k = e[k].next)
  66. {
  67. edge c = e[k];
  68. if(c.c <= 0){
  69. continue;
  70. }
  71. int m = dist[c.v];
  72. //dist[c.v] = Math.min(dist[c.v], dist[c.u] + c.c);
  73. if(dist[c.v] > dist[c.u] + c.c){
  74. dist[c.v] = dist[c.u] + c.c;
  75. if(m != dist[c.v]){
  76. father[c.v] = k;
  77.  
  78. }
  79.  
  80.  
  81. pQ.add(new point(c.v, dist[c.v]));
  82. }
  83.  
  84.  
  85. }
  86.  
  87.  
  88. }
  89. if(dist[sink] == INF){
  90. break;
  91. }
  92. int s = sink;
  93. while(father[s] != -1){
  94. smallest = Math.min(smallest, e[father[s]].c);
  95. s = e[father[s]].u;
  96. }
  97. totalFlow += smallest;
  98. s = sink;
  99. while(father[s] != -1){
  100. e[father[s]].c -= smallest;
  101. e[father[s]^1].c += smallest;
  102. s = e[father[s]].u;
  103. }
  104. }
  105. pw.println(totalFlow);
  106. pw.close();
  107.  
  108.  
  109. }
  110. static class edge
  111. {
  112. public int u, v, c, next;
  113.  
  114. edge(int _u, int _v, int _c, int _next)
  115. {
  116. u = _u; v = _v; c = _c; next = _next;
  117. }
  118. }
  119.  
  120. static edge[] e;
  121. static int N, tot, last[];
  122.  
  123. public static void init(int points)
  124. {
  125. N = points;
  126. tot = 0;
  127.  
  128. e = new edge[MAXM];
  129. last = new int[MAXN];
  130.  
  131. for (int i = 0; i < points; ++i)
  132. last[i] = -1;
  133. for(int i = 0; i < e.length; ++i){
  134. e[i] = new edge(0,0,0, 0);
  135. }
  136. }
  137.  
  138. public static void adde(int u, int v, int c)
  139. {
  140. e[tot].u = u; e[tot].v = v; e[tot].c = c; e[tot].next = last[u]; last[u] = tot++;
  141. }
  142.  
  143. public static void adde2(int u, int v, int c)
  144. {
  145. adde(u, v, c);
  146. adde(v, u, c);
  147. }
  148. static class point implements Comparable<point>{
  149. int ID;
  150. int dist;
  151. public point(int i, int j){
  152. ID = i;
  153. dist = j;
  154. }
  155. public int compareTo(point x){
  156. return dist - x.dist;
  157. }
  158. }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement