Guest User

Untitled

a guest
Nov 24th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.StringTokenizer;
  7. import java.util.Vector;
  8.  
  9. public class TransportationSystem {
  10.     int cnt1;
  11.     double cnt2, cnt3;
  12.  
  13.     public static void main(String[] args) throws NumberFormatException,
  14.             IOException {
  15.         new TransportationSystem().Solve();
  16.     }
  17.  
  18.     public void Solve() throws NumberFormatException, IOException {
  19.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  20.         StringTokenizer st;
  21.         int c = Integer.parseInt(in.readLine()), t = 0, n, r;
  22.         Vector<Point> vec;
  23.         while (c > t) {
  24.             t++;
  25.             st = new StringTokenizer(in.readLine());
  26.             n = Integer.parseInt(st.nextToken());
  27.             r = Integer.parseInt(st.nextToken());
  28.             vec = new Vector<Point>(n);
  29.             for (int i = 0; i < n; i++) {
  30.                 st = new StringTokenizer(in.readLine());
  31.                 vec.add(new Point(Integer.parseInt(st.nextToken()), Integer
  32.                         .parseInt(st.nextToken())));
  33.             }
  34.             ArrayList<Edge> edgeList = new ArrayList<Edge>(n * (n + 1));
  35.             int x1, x2, y1, y2, dx, dy;
  36.             for (int i = 0; i < vec.size(); i++)
  37.                 for (int j = i + 1; j < vec.size(); j++)
  38.                     if (i != j) {
  39.                         x1 = vec.get(i).x;
  40.                         y1 = vec.get(i).y;
  41.                         x2 = vec.get(j).x;
  42.                         y2 = vec.get(j).y;
  43.                         dx = x1 - x2;
  44.                         dy = y1 - y2;
  45.                         edgeList.add(new Edge(i, j, Math
  46.                                 .sqrt(dx * dx + dy * dy)));
  47.                     }
  48.             mst(edgeList, n, r);
  49.             System.out.printf("Case #%d: %d %d %d\n", t, cnt1,
  50.                     Math.round(cnt2), Math.round(cnt3));
  51.         }
  52.     }
  53.  
  54.     class Point {
  55.         int x, y;
  56.  
  57.         public Point(int xx, int yy) {
  58.             x = xx;
  59.             y = yy;
  60.         }
  61.     }
  62.  
  63.     public void mst(ArrayList<Edge> edgeList, int nodes, int r) {
  64.         cnt1 = 0;
  65.         cnt2 = cnt3 = 0;
  66.         DisJointSet ds = new DisJointSet(nodes);
  67.         Collections.sort(edgeList);
  68.         for (int i = 0; i < edgeList.size(); i++) {
  69.             if (!ds.isSameSet(edgeList.get(i).from, edgeList.get(i).to)) {
  70.                 ds.union(edgeList.get(i).from, edgeList.get(i).to);
  71.                 if (edgeList.get(i).cost <= r)
  72.                     cnt2 += edgeList.get(i).cost;
  73.                 else {
  74.                     cnt1++;
  75.                     cnt3 += edgeList.get(i).cost;
  76.                 }
  77.             }
  78.         }
  79.         cnt1++;
  80.     }
  81.  
  82.     class Edge implements Comparable<Edge> {
  83.         int from, to;
  84.         double cost;
  85.  
  86.         public Edge(int aa, int bb, double cc) {
  87.             from = aa;
  88.             to = bb;
  89.             cost = cc;
  90.         }
  91.  
  92.         @Override
  93.         public int compareTo(Edge o) {
  94.             return new Double(cost).compareTo(new Double(o.cost));
  95.         }
  96.     }
  97.  
  98.     class DisJointSet {
  99.         int[] parent;
  100.  
  101.         public DisJointSet(int size) {
  102.             parent = new int[size];
  103.             for (int i = 0; i < size; i++)
  104.                 parent[i] = i;
  105.         }
  106.  
  107.         public boolean isSameSet(int first, int second) {
  108.             return findParent(first) == findParent(second);
  109.         }
  110.  
  111.         public int findParent(int n) {
  112.             if (parent[n] == n)
  113.                 return n;
  114.             return parent[n] = findParent(parent[n]);
  115.         }
  116.  
  117.         public void union(int first, int second) {
  118.             parent[findParent(first)] = findParent(second);
  119.         }
  120.  
  121.         public int numOfDisSets() {
  122.             int cnt = 0;
  123.             for (int i = 0; i < parent.length; i++)
  124.                 if (parent[i] == i)
  125.                     cnt++;
  126.             return cnt;
  127.         }
  128.     }
  129. }
Add Comment
Please, Sign In to add comment