Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import java.util.HashSet;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class Radio {
  8.  
  9. /**
  10. * @param args
  11. */
  12. public static void main(String[] args) {
  13. Scanner in = new Scanner(System.in);
  14. int R=in.nextInt();
  15. int S=in.nextInt();
  16. int N=in.nextInt();
  17. Stanica[] stanice = new Stanica[N+2];
  18. stanice[0]=new Stanica(0,0);
  19. for (int i=1;i<=N;i++) stanice[i]=new Stanica(in.nextInt(),in.nextInt());
  20. stanice[N+1]=new Stanica(R,S);
  21.  
  22.  
  23. int U=1000000000;
  24. int L=0;
  25. while(L<U){
  26.  
  27. int probajRjesenje= (U+L)/2;
  28. int[][] veze = new int[N+2][N+2];
  29. for(int i=0;i<N+2;i++)
  30. for(int j=0;j<N+2;j++)
  31. {
  32. if (i==j) continue;
  33. veze[i][j]=0;
  34.  
  35. }
  36.  
  37.  
  38.  
  39. for(int i=0;i<N+2;i++)
  40. for(int j=0;j<N+2;j++)
  41. {
  42. if (i==j) continue;
  43. if ((udaljenost(stanice[i],stanice[j])/2)<=probajRjesenje) veze[i][j]=1;
  44.  
  45. }
  46. Queue<Integer> kju = new LinkedList<Integer>();
  47. kju.add(0);
  48. boolean moze=false;
  49. HashSet<Integer> vecbio = new HashSet<Integer>();
  50.  
  51. while(true){
  52. if (kju.isEmpty()) break;
  53. int trenutni=kju.poll();
  54. vecbio.add(trenutni);
  55. if (trenutni==N+1) {moze=true; break;}
  56. for (int k=0;k<N+2;k++){
  57. if (veze[trenutni][k]==1) if (!vecbio.contains(k)) {kju.add(k); vecbio.add(k);}
  58. }
  59.  
  60. }
  61.  
  62. kju.clear();
  63. vecbio.clear();
  64. if (moze) U=probajRjesenje;
  65. else L=probajRjesenje+1;
  66.  
  67.  
  68. }
  69. System.out.println(L);
  70. }
  71.  
  72. private static int udaljenost(Stanica a, Stanica b) {
  73.  
  74. return Math.abs(a.r-b.r)+Math.abs(a.s-b.s);
  75. }
  76.  
  77. }
  78.  
  79.  
  80.  
  81. class Stanica{
  82. public int r;
  83. public int s;
  84.  
  85.  
  86. public Stanica() {
  87. // TODO Auto-generated constructor stub
  88. }
  89.  
  90. public Stanica(int r, int s) {
  91. this.r=r;
  92. this.s=s;
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement