Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package CCC;
- import java.io.*;
- import java.util.*;
- public class ccc02s2p5 {
- static int V,E;
- Edge[] edge;
- int[] parent;
- Edge[] mst;
- public class Edge implements Comparable<Edge> {
- int bvx, bvy, evx, evy, cost;
- @Override
- public int compareTo(Edge o) {
- return this.cost-o.cost;
- }
- }
- public ccc02s2p5(int v, int e) {
- V = v;
- E = e;
- parent = new int[V];
- for (int i = 0; i < V; i++) parent[i] = -1;
- edge = new Edge[E];
- for (int i = 0; i < E; i++) edge[i] = new Edge();
- mst = new Edge[V-1];
- for (int i = 0; i < V-1; i++) mst[i] = new Edge();
- }
- public int find(int v) {
- if (parent[v] == -1) return v;
- else return find(parent[v]);
- }
- public void union(int pb, int pe) {
- parent[pb] = pe;
- }
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- StringTokenizer st = new StringTokenizer(br.readLine());
- int V = Integer.parseInt(st.nextToken());
- int E = 0;
- for (int i=1; i<V; i++) {
- E += i;
- }
- ccc02s2p5 graph = new ccc02s2p5(V,E);
- int[] positions_x = new int[V];
- int[] positions_y = new int[V];
- for (int i=0; i<V; i++) {
- st = new StringTokenizer(br.readLine());
- positions_x[i] = Integer.parseInt(st.nextToken());
- positions_y[i] = Integer.parseInt(st.nextToken());
- }
- double[][] distances = new double[V][V];
- for (int i=0; i<V; i++) {
- for (int j=0; j<V; j++) {
- distances[i][j] = Math.sqrt(Math.pow((positions_x[i]-positions_x[j]),2)+Math.pow((positions_y[i]-positions_y[j]),2));
- }
- }
- for (int i = 0; i<V-1; i++) {
- for (int j=i+1; j<V; j++) {
- graph.edge[i].bvx = positions_x[i];
- graph.edge[i].bvy = positions_y[i];
- graph.edge[i].evx = positions_x[j];
- graph.edge[i].evy = positions_y[j];
- graph.edge[i].cost = (int) ((distances[graph.edge[i].bv][graph.edge[i].ev]) * 1000);
- }
- }
- Arrays.sort(graph.edge);
- int j = 0;
- for (int i = 0; i < E; i++) {
- int bv = graph.edge[i].bv, ev = graph.edge[i].ev;
- int pb = graph.find(bv), pe = graph.find(ev);
- if (pb != pe) {
- graph.mst[j].bv = bv;
- graph.mst[j].ev = ev;
- graph.mst[j].cost = graph.edge[i].cost;
- graph.union(pb, pe);
- j++;
- }
- }
- int total = 0;
- for (int i = 0; i < V-1; i++) {
- System.out.println(graph.mst[i].bv + " " + graph.mst[i].ev + " " + graph.mst[i].cost);
- total += graph.mst[i].cost;
- }
- System.out.println(total);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement