Cod sursa(job #1705602)

Utilizator greenroFlorin Calota greenro Data 20 mai 2016 20:37:29
Problema Arbore partial de cost minim Scor 20
Compilator java Status done
Runda Arhiva educationala Marime 3.18 kb
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

class Main {

    public static void main(String[] args) throws FileNotFoundException, IOException {
        BufferedReader in = new BufferedReader(new FileReader("apm.in"));
        String temp[] = in.readLine().split(" ");
        int n = Integer.parseInt(temp[0]);
        int m = Integer.parseInt(temp[1]);
        Edge[] E = new Edge[m];
        for (int i = 0; i < m; i++) {
            temp = in.readLine().split(" ");
            int x = Integer.parseInt(temp[0]);
            int y = Integer.parseInt(temp[1]);
            int c = Integer.parseInt(temp[2]);
            E[i] = new Edge(x, y, c);
        }
        Arrays.sort(E, new Comparator<Edge>() {
            @Override
            public int compare(Edge o1, Edge o2) {
                return (int) (o1.cost - o2.cost);
            }
        });
        kruskal(E);
        PrintWriter out = new PrintWriter("apm.out");
        out.println(cost);
        out.println(nama);
        for (int i = 0; i < nama; i++) {
            out.println(AMA[i]);
        }
        out.close();
    }
    static long cost;
    static Edge[] AMA;
    static int nama;

    private static void kruskal(Edge[] E) {
        int L = E.length + 2;
        AMA = new Edge[L];
        List<List<Integer>> al = new ArrayList<>();
        for (int i = 0; i < L; i++) {
            al.add(new ArrayList<Integer>());
            al.get(i).add(i);
        }
        int index = 0;
        for (Edge e : E) {
            int src = e.n1;
            int dest = e.n2;
            int srcIndex = -1;
            int destIndex = -1;
            for (int i = 0; i < L; i++) {
                if (al.get(i).contains(src)) {
                    srcIndex = i;
                    if (al.get(i).contains(dest)) {
                        destIndex = i;
                        break;
                    } else {
                        for (int j = 0; j < L; j++) {
                            if (al.get(j).contains(dest)) {
                                destIndex = j;
                                break;
                            }
                        }
                    }
                    break;
                }
            }
            if (srcIndex != destIndex) {
                nama++;
                AMA[index++] = new Edge(e.n1, e.n2, e.cost);
                cost += e.cost;
                if (srcIndex != -1 && destIndex != -1) {
                    al.get(srcIndex).addAll(al.get(destIndex));
                    al.get(destIndex).addAll(al.get(srcIndex));
                }
            }
        }
    }

    static class Edge {

        int n1;
        int n2;
        long cost;

        public Edge(int n1, int n2, long cost) {
            this.n1 = n1;
            this.n2 = n2;
            this.cost = cost;
        }

        @Override
        public String toString() {
            return n2 + " " + n1;
        }

    }
}