Advertisement
Yahya-Ak-Ayoub

aa

Nov 7th, 2020
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import static java.lang.Integer.parseInt;
  9. import java.math.BigInteger;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12. import java.util.Comparator;
  13. import java.util.PriorityQueue;
  14. import java.util.StringTokenizer;
  15.  
  16. public class MST_Minimum_Spanning_Tree {
  17.  
  18.     static BufferedReader reader;
  19.     static StringTokenizer tok;
  20.     static int n, m;
  21.     static double cost[], wieght[];
  22.     static ArrayList<pair> graph[];
  23.     static PriorityQueue<pair2> q;
  24.     static boolean visited[];
  25.  
  26.     public static void main(String[] args) throws IOException {
  27. //        BufferedWriter writer = new BufferedWriter(new FileWriter("etex.txt"));
  28. //        writer.write(100000 + " " + 100000);
  29. //        writer.newLine();
  30. //        for (int i = 1; i < 100000; i++) {
  31. //            writer.write(i + " " + (i + 1) + " " + 1000000);
  32. //            writer.newLine();
  33. //            //System.out.println(i + " " + (i + 1) + " " + 1000000);
  34. //        }
  35.         Input();
  36.         sol();
  37.     }
  38.  
  39.     public static void Input() throws IOException {
  40.        reader = new BufferedReader(new InputStreamReader(System.in));
  41.       //    reader = new BufferedReader(new FileReader("etex.txt"));
  42.  
  43.         tok = new StringTokenizer(reader.readLine());
  44.         n = parseInt(tok.nextToken());
  45.         m = parseInt(tok.nextToken());
  46.         graph = new ArrayList[n + 1];
  47.         for (int i = 1; i <= n; i++) {
  48.             graph[i] = new ArrayList<pair>();
  49.         }
  50.         for (int i = 1; i <= m; i++) {
  51.             tok = new StringTokenizer(reader.readLine());
  52.             int x = parseInt(tok.nextToken()), y = parseInt(tok.nextToken()), w = parseInt(tok.nextToken());
  53.            System.out.println(x + "  "+y);
  54.  
  55.             graph[x].add(new pair(y, w));
  56.             graph[y].add(new pair(x, w));
  57.         }
  58.     }
  59.  
  60.     public static void sol() {
  61.  
  62.         cost = new double[n + 1];
  63.         wieght = new double[n + 1];
  64.  
  65.         visited = new boolean[n + 1];
  66.         q = new PriorityQueue<pair2>(Comparator.comparing((pair2 obj) -> obj.weight));
  67.         q.add(new pair2(1, 0));
  68.         visited[1] = true;
  69.  
  70.         while (!q.isEmpty()) {
  71.  
  72.             pair2 obj = q.poll();
  73.  
  74.             for (pair i : graph[obj.node]) {
  75.                 if (!visited[i.node]) {
  76.                     visited[i.node] = true;
  77.  
  78.                     cost[i.node] = (i.weight + obj.weight);
  79.                     wieght[i.node] = i.weight;
  80.                     cost[0] += i.weight;
  81.                     q.add(new pair2(i.node, cost[i.node]));
  82.                 } else if (cost[i.node]  > i.weight + obj.weight) {
  83.                     cost[0] -= wieght[i.node];
  84.                     wieght[i.node] = i.weight;
  85.                     cost[i.node] = (i.weight + obj.weight);
  86.                     cost[0] += wieght[i.node];
  87.                     q.add(new pair2(i.node, cost[i.node]));
  88.                 }
  89.             }
  90.  
  91.         }
  92.  
  93.         System.out.println((int) (cost[0]));
  94.  
  95.     }
  96.  
  97.     static class pair {
  98.  
  99.         int node, weight;
  100.  
  101.         public pair(int node, int weight) {
  102.             this.node = node;
  103.             this.weight = weight;
  104.         }
  105.  
  106.     }
  107.  
  108.     static class pair2 {
  109.  
  110.         int node;
  111.         double weight;
  112.  
  113.         public pair2(int node, double weight) {
  114.             this.node = node;
  115.             this.weight = weight;
  116.         }
  117.  
  118.     }
  119. }
  120.  
  121. /*
  122.  
  123. 4 5
  124. 1 2 10
  125. 2 3 15
  126. 1 3 5
  127. 4 2 2
  128. 4 3 40
  129.  
  130.  
  131. 4 5
  132. 1 2 10
  133. 2 3 1
  134. 1 3 5
  135. 4 2 2
  136. 4 3 40
  137.  
  138. 4 5
  139. 1 2 10
  140. 2 3 4
  141. 1 3 5
  142. 4 2 2
  143. 4 3 1
  144.  
  145.  
  146. 4 5
  147. 1 2 2
  148. 2 3 2
  149. 1 3 2
  150. 4 2 2
  151. 4 3 3
  152.  
  153.  */
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement