Advertisement
Guest User

357

a guest
May 24th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5.  
  6. public class Solution {
  7.  
  8.     private static int dist(Vertex u, Vertex v){
  9.         int x = u.x - v.x;
  10.         int y = u.y - v.y;
  11.         return x * x + y * y;
  12.     }
  13.  
  14.     public static void main(String[] args) throws IOException {
  15.         PrintWriter writer = new PrintWriter(new BufferedOutputStream(System.out));
  16.         BufferedReader in = new BufferedReader(new FileReader("input.txt"));
  17.  
  18.         String[] arr = in.readLine().split(" ");
  19.         int n = Integer.parseInt(arr[0]);
  20.  
  21.         List<Vertex> g = new ArrayList<Vertex>();
  22.  
  23.         for (int i = 0; i < n; i++) {
  24.             arr = in. readLine().split(" ");
  25.             int x = Integer.parseInt(arr[0]);
  26.             int y = Integer.parseInt(arr[1]);
  27.             g.add(new Vertex(x, y));
  28.         }
  29.  
  30.         arr = in.readLine().split(" ");
  31.         int m = Integer.parseInt(arr[0]);
  32.  
  33.         boolean[][] edges = new boolean[n][n];
  34.  
  35.         for (int i = 0; i < m; i++) {
  36.             arr = in.readLine().split(" ");
  37.             int u = Integer.parseInt(arr[0]) - 1;
  38.             int v = Integer.parseInt(arr[1]) - 1;
  39.             edges[u][v] = true;
  40.             edges[v][u] = true;
  41.         }
  42.  
  43.         int[] d = new int[n];
  44.         int[] p = new int[n];
  45.         boolean[] used = new boolean[n];
  46.  
  47.         Arrays.fill(d, Integer.MAX_VALUE);
  48.  
  49.         d[0] = 0;
  50.  
  51.         for (int k = 0; k < n; k++){
  52.             int i = -1;
  53.             for (int j = 0; j < n; j++) {
  54.                 if (!used[j] && (i == -1 || d[j] < d[i])) {
  55.                     i = j;
  56.                 }
  57.             }
  58.             used[i] = true;
  59.             for (int j = 0; j < n; j++){
  60.  
  61.                 int w = 0;
  62.                 if (!edges[i][j]) {
  63.                     w = dist(g.get(i), g.get(j));
  64.                 }
  65.  
  66.                 if (!used[j] && d[j] > w){
  67.                     d[j] = w;
  68.                     p[j] = i;
  69.                 }
  70.             }
  71.         }
  72.  
  73.         for (int i = 1; i < n; i++) {
  74.             if (!edges[p[i]][i]) {
  75.                 writer.println((p[i] + 1) + " " + (i + 1));
  76.             }
  77.         }
  78.  
  79.         writer.close();
  80.     }
  81.  
  82.     private static final class Vertex {
  83.         private int x;
  84.         private int y;
  85.  
  86.         public Vertex(int x, int y) {
  87.             this.x = x;
  88.             this.y = y;
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement