Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashSet;
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Scanner;
- public class Radio {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int R=in.nextInt();
- int S=in.nextInt();
- int N=in.nextInt();
- Stanica[] stanice = new Stanica[N+2];
- stanice[0]=new Stanica(0,0);
- for (int i=1;i<=N;i++) stanice[i]=new Stanica(in.nextInt(),in.nextInt());
- stanice[N+1]=new Stanica(R,S);
- int U=1000000000;
- int L=0;
- while(L<U){
- int probajRjesenje= (U+L)/2;
- int[][] veze = new int[N+2][N+2];
- for(int i=0;i<N+2;i++)
- for(int j=0;j<N+2;j++)
- {
- if (i==j) continue;
- veze[i][j]=0;
- }
- for(int i=0;i<N+2;i++)
- for(int j=0;j<N+2;j++)
- {
- if (i==j) continue;
- if ((udaljenost(stanice[i],stanice[j])/2)<=probajRjesenje) veze[i][j]=1;
- }
- Queue<Integer> kju = new LinkedList<Integer>();
- kju.add(0);
- boolean moze=false;
- HashSet<Integer> vecbio = new HashSet<Integer>();
- while(true){
- if (kju.isEmpty()) break;
- int trenutni=kju.poll();
- vecbio.add(trenutni);
- if (trenutni==N+1) {moze=true; break;}
- for (int k=0;k<N+2;k++){
- if (veze[trenutni][k]==1) if (!vecbio.contains(k)) {kju.add(k); vecbio.add(k);}
- }
- }
- kju.clear();
- vecbio.clear();
- if (moze) U=probajRjesenje;
- else L=probajRjesenje+1;
- }
- System.out.println(L);
- }
- private static int udaljenost(Stanica a, Stanica b) {
- return Math.abs(a.r-b.r)+Math.abs(a.s-b.s);
- }
- }
- class Stanica{
- public int r;
- public int s;
- public Stanica() {
- // TODO Auto-generated constructor stub
- }
- public Stanica(int r, int s) {
- this.r=r;
- this.s=s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement