Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.StringTokenizer;
- import java.util.Vector;
- public class TransportationSystem {
- int cnt1;
- double cnt2, cnt3;
- public static void main(String[] args) throws NumberFormatException,
- IOException {
- new TransportationSystem().Solve();
- }
- public void Solve() throws NumberFormatException, IOException {
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- StringTokenizer st;
- int c = Integer.parseInt(in.readLine()), t = 0, n, r;
- Vector<Point> vec;
- while (c > t) {
- t++;
- st = new StringTokenizer(in.readLine());
- n = Integer.parseInt(st.nextToken());
- r = Integer.parseInt(st.nextToken());
- vec = new Vector<Point>(n);
- for (int i = 0; i < n; i++) {
- st = new StringTokenizer(in.readLine());
- vec.add(new Point(Integer.parseInt(st.nextToken()), Integer
- .parseInt(st.nextToken())));
- }
- ArrayList<Edge> edgeList = new ArrayList<Edge>(n * (n + 1));
- int x1, x2, y1, y2, dx, dy;
- for (int i = 0; i < vec.size(); i++)
- for (int j = i + 1; j < vec.size(); j++)
- if (i != j) {
- x1 = vec.get(i).x;
- y1 = vec.get(i).y;
- x2 = vec.get(j).x;
- y2 = vec.get(j).y;
- dx = x1 - x2;
- dy = y1 - y2;
- edgeList.add(new Edge(i, j, Math
- .sqrt(dx * dx + dy * dy)));
- }
- mst(edgeList, n, r);
- System.out.printf("Case #%d: %d %d %d\n", t, cnt1,
- Math.round(cnt2), Math.round(cnt3));
- }
- }
- class Point {
- int x, y;
- public Point(int xx, int yy) {
- x = xx;
- y = yy;
- }
- }
- public void mst(ArrayList<Edge> edgeList, int nodes, int r) {
- cnt1 = 0;
- cnt2 = cnt3 = 0;
- DisJointSet ds = new DisJointSet(nodes);
- Collections.sort(edgeList);
- for (int i = 0; i < edgeList.size(); i++) {
- if (!ds.isSameSet(edgeList.get(i).from, edgeList.get(i).to)) {
- ds.union(edgeList.get(i).from, edgeList.get(i).to);
- if (edgeList.get(i).cost <= r)
- cnt2 += edgeList.get(i).cost;
- else {
- cnt1++;
- cnt3 += edgeList.get(i).cost;
- }
- }
- }
- cnt1++;
- }
- class Edge implements Comparable<Edge> {
- int from, to;
- double cost;
- public Edge(int aa, int bb, double cc) {
- from = aa;
- to = bb;
- cost = cc;
- }
- @Override
- public int compareTo(Edge o) {
- return new Double(cost).compareTo(new Double(o.cost));
- }
- }
- class DisJointSet {
- int[] parent;
- public DisJointSet(int size) {
- parent = new int[size];
- for (int i = 0; i < size; i++)
- parent[i] = i;
- }
- public boolean isSameSet(int first, int second) {
- return findParent(first) == findParent(second);
- }
- public int findParent(int n) {
- if (parent[n] == n)
- return n;
- return parent[n] = findParent(parent[n]);
- }
- public void union(int first, int second) {
- parent[findParent(first)] = findParent(second);
- }
- public int numOfDisSets() {
- int cnt = 0;
- for (int i = 0; i < parent.length; i++)
- if (parent[i] == i)
- cnt++;
- return cnt;
- }
- }
- }
Add Comment
Please, Sign In to add comment