Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.22 KB | None | 0 0
  1. class point{
  2.     int x;
  3.     int y;
  4.     point pi;
  5.     double key;
  6.      int num;
  7.      int arraypos;
  8.     point(int x,int y,point pi,double key,int nu,int apos){
  9.        this.num=nu;
  10.         this.x=x;
  11.         this.y=y;
  12.         this.pi=pi;
  13.         this.key=key;
  14.         this.arraypos=apos;
  15.     }
  16.     void set(int x,int y,point pi,double key,int nu,int apos){
  17.        this.num=nu;
  18.         this.x=x;
  19.         this.y=y;
  20.         this.pi=pi;
  21.         this.key=key;
  22.         this.arraypos=apos;
  23.     }
  24. }
  25. public class Main {
  26.  
  27.     Main p;
  28.      int heapsize=0,findcount=0;
  29.  
  30.          
  31.     /**
  32.      * @param args the command line arguments
  33.      */
  34.      double calweight(point u,point v){
  35.          int x1=u.x;
  36.          int y1=u.y;
  37.          int x2=v.x;
  38.          int y2=v.y;
  39.  
  40.          int dif1=0,dif2=0;
  41.      if(x1>x2)
  42.                 dif1=x1-x2;
  43.                 else
  44.                  dif1=x2-x1;
  45.                 if(y1>y2)
  46.                 dif2=y1-y2;
  47.                 else
  48.                  dif2=y2-y1;
  49.  
  50.  
  51.                return java.lang.Math.sqrt(dif1^2+dif2^2);
  52.  
  53.            }
  54.      int contains(point[] q,point v){
  55.          for(int i=1;i<=heapsize;i++){
  56.              if(q[i]==v)
  57.                  return 1;
  58.                  }
  59.          return 0;
  60.     }
  61.  
  62.     public static void main(String[] args) {
  63.         Main f=new Main();
  64.         Map<Integer,ArrayList<point>> edges=new HashMap<Integer,ArrayList<point>>();
  65.          
  66.          int x1,x2,y1,y2,dif1,dif2;
  67.  
  68.          int n=10,m=20,k=80;
  69.          point[] Q=new point[n+1];
  70.          Random rand1=new  Random();
  71.          Random rand2=new  Random();
  72.  
  73.         for(int i=1;i<=n;i++)
  74.         {
  75.  
  76.             x1=rand1.nextInt(k+1);
  77.             y1=rand2.nextInt(k+1);
  78.            Q[i]=new point(x1,y1,null,9999,i,i);
  79.  
  80.         }
  81.          rand1=new  Random();
  82.          rand2=new  Random();
  83.          for(int j=1;j<=m;j++){
  84.              int p1=0,p2=0;
  85.              while(p1==0 || p2==0){
  86.              p1=rand1.nextInt(n+1);
  87.              p2=rand1.nextInt(n+1);
  88.              }
  89.              if(edges.containsKey(p1)){
  90.                  ArrayList x=edges.get(p1);
  91.                  x.add(Q[p2]);
  92.              }
  93.  else{
  94.                  ArrayList<point> y=new ArrayList<point>();
  95.                  y.add(Q[p2]);
  96.                  edges.put(p1,y);
  97.  }
  98.          }
  99.  
  100.          f.heapsize=Q.length-1;
  101.          while(f.heapsize>0){
  102.              point u=f.extractmin(Q);
  103.              if(u.pi!=null)
  104.                  System.out.println(u.num+" "+u.pi.num);
  105.              else
  106.                System.out.println(u.num);
  107.              if(edges.containsKey(u.num)){
  108.  
  109.                  for(point v:edges.get(u.num)){
  110.                        
  111.                      if(f.calweight(u, v)<v.key){
  112.                    
  113.                      v.pi=u;
  114.                      v.key=f.calweight(u, v);
  115.                      f.decreasekey(Q,v.arraypos);
  116.                      }
  117.  
  118.                  }
  119.  
  120.  
  121.              }
  122.  
  123.          }
  124.  
  125.     }
  126.  void decreasekey(point[] a,int i){
  127.        
  128.        
  129.         while(i>1 && a[i].key<a[i/2].key){
  130.           point temp=new point(a[i].x,a[i].y,a[i].pi,a[i].key,a[i].num,a[i].arraypos);
  131.           a[i].set(a[i/2].x, a[i/2].y, a[i/2].pi,a[i/2].key,a[i].num,a[i/2].arraypos);
  132.           a[i/2].set(temp.x, temp.y, temp.pi,temp.key,a[i/2].num,a[i].arraypos);
  133.           i=i/2;
  134.      
  135.         }
  136.     }
  137.  void max_heapify(point[] a,int i){
  138.        int l=2*i;
  139.        int large=i;
  140.        int r=2*i+1;
  141.        if(l<a.length && a[l].key<a[i].key)
  142.            large=l;
  143.        else
  144.            large=i;
  145.         if(r<a.length && a[r].key<a[i].key)
  146.            large=r;
  147.        if(large!=i){
  148.            point temp=new point(a[i].x,a[i].y,a[i].pi,a[i].key,a[i].num,a[i].arraypos);
  149.            a[i].set(a[large].x, a[large].y, a[large].pi,a[large].key,a[i].num,a[large].arraypos);
  150.           a[large].set(temp.x, temp.y, temp.pi,temp.key,a[large].num,a[i].arraypos);
  151.    
  152.            max_heapify(a, large);
  153. }
  154. }
  155.   point extractmin(point[] a){
  156.        point min=new point(a[1].x,a[1].y,a[1].pi,a[1].key,a[1].num,a[1].arraypos);
  157.        a[1].set(a[heapsize].x, a[heapsize].y,a[heapsize].pi,a[heapsize].key,a[heapsize].num,1);
  158.        heapsize=heapsize-1;
  159.         max_heapify(a,1);
  160.        return min;
  161.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement