Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import static java.lang.Integer.parseInt;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.PriorityQueue;
- import java.util.StringTokenizer;
- public class MST_Minimum_Spanning_Tree {
- static BufferedReader reader;
- static StringTokenizer tok;
- static int n, m;
- static double cost[], wieght[];
- static ArrayList<pair> graph[];
- static PriorityQueue<pair2> q;
- static boolean visited[];
- public static void main(String[] args) throws IOException {
- // BufferedWriter writer = new BufferedWriter(new FileWriter("etex.txt"));
- // writer.write(100000 + " " + 100000);
- // writer.newLine();
- // for (int i = 1; i < 100000; i++) {
- // writer.write(i + " " + (i + 1) + " " + 1000000);
- // writer.newLine();
- // //System.out.println(i + " " + (i + 1) + " " + 1000000);
- // }
- Input();
- sol();
- }
- public static void Input() throws IOException {
- reader = new BufferedReader(new InputStreamReader(System.in));
- // reader = new BufferedReader(new FileReader("etex.txt"));
- tok = new StringTokenizer(reader.readLine());
- n = parseInt(tok.nextToken());
- m = parseInt(tok.nextToken());
- graph = new ArrayList[n + 1];
- for (int i = 1; i <= n; i++) {
- graph[i] = new ArrayList<pair>();
- }
- for (int i = 1; i <= m; i++) {
- tok = new StringTokenizer(reader.readLine());
- int x = parseInt(tok.nextToken()), y = parseInt(tok.nextToken()), w = parseInt(tok.nextToken());
- System.out.println(x + " "+y);
- graph[x].add(new pair(y, w));
- graph[y].add(new pair(x, w));
- }
- }
- public static void sol() {
- cost = new double[n + 1];
- wieght = new double[n + 1];
- visited = new boolean[n + 1];
- q = new PriorityQueue<pair2>(Comparator.comparing((pair2 obj) -> obj.weight));
- q.add(new pair2(1, 0));
- visited[1] = true;
- while (!q.isEmpty()) {
- pair2 obj = q.poll();
- for (pair i : graph[obj.node]) {
- if (!visited[i.node]) {
- visited[i.node] = true;
- cost[i.node] = (i.weight + obj.weight);
- wieght[i.node] = i.weight;
- cost[0] += i.weight;
- q.add(new pair2(i.node, cost[i.node]));
- } else if (cost[i.node] > i.weight + obj.weight) {
- cost[0] -= wieght[i.node];
- wieght[i.node] = i.weight;
- cost[i.node] = (i.weight + obj.weight);
- cost[0] += wieght[i.node];
- q.add(new pair2(i.node, cost[i.node]));
- }
- }
- }
- System.out.println((int) (cost[0]));
- }
- static class pair {
- int node, weight;
- public pair(int node, int weight) {
- this.node = node;
- this.weight = weight;
- }
- }
- static class pair2 {
- int node;
- double weight;
- public pair2(int node, double weight) {
- this.node = node;
- this.weight = weight;
- }
- }
- }
- /*
- 4 5
- 1 2 10
- 2 3 15
- 1 3 5
- 4 2 2
- 4 3 40
- 4 5
- 1 2 10
- 2 3 1
- 1 3 5
- 4 2 2
- 4 3 40
- 4 5
- 1 2 10
- 2 3 4
- 1 3 5
- 4 2 2
- 4 3 1
- 4 5
- 1 2 2
- 2 3 2
- 1 3 2
- 4 2 2
- 4 3 3
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement