Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class PriorityQueueP {
  5. //https://pastebin.com/Nhe9rTN5
  6. public static class Edge implements Comparable<Edge>{
  7. int s, d;long wt; boolean v;
  8. public Edge(int s, int d, int wt, boolean v)
  9. {this.s = s; this.d = d; this.wt = wt; this.v = v;}
  10. @Override
  11. public int compareTo(Edge o) {
  12. return Long.compare(this.wt, o.wt);
  13. }
  14. }
  15.  
  16. public static class DisjointSet{
  17. int par[], N;
  18. public DisjointSet(int N) {
  19. this.N= N;
  20. par = new int[N+1];
  21. for(int i = 1; i<=N; i++)
  22. par[i] = i;
  23. }
  24.  
  25. public int find(int x)
  26. {
  27. if(par[x] == x) {
  28. return x;
  29. }
  30. return par[x] = find(par[x]);
  31. }
  32.  
  33. public void union(int x, int y) {
  34. par[x] = y;
  35. }
  36. }
  37.  
  38. public static void main(String[] args) throws IOException {
  39. //N is planet
  40. //M is city
  41. int N = readInt(), M = readInt(), P = readInt(), Q = readInt(); long total = 0, MST = 0;
  42. PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
  43. for(int i = 1; i<=P; i++)
  44. pq.add(new Edge(readInt(), readInt(), readInt(), false));
  45. for(int i = 1; i<=Q; i++)
  46. pq.add(new Edge(readInt(), readInt(), readInt(), true));
  47. DisjointSet Ns = new DisjointSet(N), Ms = new DisjointSet(M);
  48.  
  49. while(!pq.isEmpty()) {
  50. Edge e = pq.poll();
  51. if(!e.v) {
  52. total += e.wt*N;
  53. if(Ms.N != 0 && Ms.find(e.d) != Ms.find(e.s)) {
  54. MST += Ns.N*e.wt;
  55. Ms.union(Ms.find(e.d), Ms.find(e.s));
  56. Ms.N -= 1;
  57. }
  58. }
  59. else {
  60. total += e.wt*M;
  61. if(Ns.N != 0 && Ns.find(e.d) != Ns.find(e.s)) {
  62. MST += Ms.N*e.wt;
  63. Ns.union(Ns.find(e.d), Ns.find(e.s));
  64. Ns.N -= 1;
  65. }
  66. }
  67. }
  68. println(total-MST);
  69. exit();
  70. }
  71.  
  72. final private static int BUFFER_SIZE = 1 << 16;
  73. private static DataInputStream din = new DataInputStream(System.in);
  74. private static byte[] buffer = new byte[BUFFER_SIZE];
  75. private static int bufferPointer = 0, bytesRead = 0;
  76. static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
  77.  
  78. public static String readLine() throws IOException {
  79. byte[] buf = new byte[64]; // line length
  80. int cnt = 0, c;
  81. while ((c = Read()) != -1) {
  82. if (c == '\n')
  83. break;
  84. buf[cnt++] = (byte) c;
  85. }
  86. return new String(buf, 0, cnt);
  87. }
  88.  
  89. public static String read() throws IOException {
  90. byte[] ret = new byte[1024];
  91. int idx = 0;
  92. byte c = Read();
  93. while (c <= ' ') {
  94. c = Read();
  95. }
  96. do {
  97. ret[idx++] = c;
  98. c = Read();
  99. } while (c != -1 && c != ' ' && c != '\n' && c != '\r');
  100. return new String(ret, 0, idx);
  101. }
  102.  
  103. public static int readInt() throws IOException {
  104. int ret = 0;
  105. byte c = Read();
  106. while (c <= ' ')
  107. c = Read();
  108. boolean neg = (c == '-');
  109. if (neg)
  110. c = Read();
  111. do {
  112. ret = ret * 10 + c - '0';
  113. } while ((c = Read()) >= '0' && c <= '9');
  114.  
  115. if (neg)
  116. return -ret;
  117. return ret;
  118. }
  119.  
  120. public static long readLong() throws IOException {
  121. long ret = 0;
  122. byte c = Read();
  123. while (c <= ' ')
  124. c = Read();
  125. boolean neg = (c == '-');
  126. if (neg)
  127. c = Read();
  128. do {
  129. ret = ret * 10 + c - '0';
  130. } while ((c = Read()) >= '0' && c <= '9');
  131. if (neg)
  132. return -ret;
  133. return ret;
  134. }
  135.  
  136. public static double readDouble() throws IOException {
  137. double ret = 0, div = 1;
  138. byte c = Read();
  139. while (c <= ' ')
  140. c = Read();
  141. boolean neg = (c == '-');
  142. if (neg)
  143. c = Read();
  144.  
  145. do {
  146. ret = ret * 10 + c - '0';
  147. } while ((c = Read()) >= '0' && c <= '9');
  148.  
  149. if (c == '.') {
  150. while ((c = Read()) >= '0' && c <= '9') {
  151. ret += (c - '0') / (div *= 10);
  152. }
  153. }
  154.  
  155. if (neg)
  156. return -ret;
  157. return ret;
  158. }
  159.  
  160. private static void fillBuffer() throws IOException {
  161. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  162. if (bytesRead == -1)
  163. buffer[0] = -1;
  164. }
  165.  
  166. private static byte Read() throws IOException {
  167. if (bufferPointer == bytesRead)
  168. fillBuffer();
  169. return buffer[bufferPointer++];
  170. }
  171.  
  172. public void close() throws IOException {
  173. if (din == null)
  174. return;
  175. din.close();
  176. }
  177.  
  178. static void print(Object o) {
  179. pr.print(o);
  180. }
  181.  
  182. static void println(Object o) {
  183. pr.println(o);
  184. }
  185.  
  186. static void flush() {
  187. pr.flush();
  188. }
  189.  
  190. static void println() {
  191. pr.println();
  192. }
  193.  
  194. static void exit() throws IOException {
  195. din.close();
  196. pr.close();
  197. System.exit(0);
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement