Advertisement
FahimFaisal

WuhanDijkstra

Mar 10th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package Contest;
  7.  
  8. import java.io.File;
  9. import java.util.PriorityQueue;
  10. import java.util.Scanner;
  11.  
  12. /**
  13.  *
  14.  * @author acer
  15.  */
  16. public class wuhanDijkstra {
  17.    
  18.     public static int [] d;
  19.     public static int [] parent;
  20.     public static int [] color;
  21.     public static int [][] matrix;
  22.     public static int node;
  23.      public static PriorityQueue<Integer> q =new PriorityQueue<Integer>();
  24.    
  25.      public static void main(String[] args) {
  26.        
  27.    
  28.         try{
  29.             //Scanner scn=new Scanner(new File("F:\\CSE_BRACU\\Courses_TAKEN\\LABS\\Algorithm_221\\Ins.txt"));
  30.            
  31.            Scanner kb=new Scanner(System.in);
  32.            
  33.             node=kb.nextInt();
  34.            
  35.             int edge=kb.nextInt();
  36.            
  37.             matrix=new int[node][node];
  38.             color=new int[node];
  39.             parent=new int[node];
  40.             d=new int[node];
  41.             String splitA[];
  42.             int k=0;
  43.             while(k<edge)
  44.             {
  45.             //String line=kb.nextLine();
  46.             //splitA=line.split(",");
  47.             int x=kb.nextInt();
  48.             int y=kb.nextInt();
  49.             int w=kb.nextInt();
  50.             matrix[x][y]=w;
  51.      
  52.             k++;
  53.             }
  54.         /* for(int i=0;i<node;i++) {
  55.                 for(int j=0;j<node;j++) {
  56.                     System.out.print(matrix[i][j]+",");
  57.                 }
  58.                 System.out.println();
  59.             }*/
  60.       dijkstra(0);
  61.            
  62.         } catch (Exception ex) {
  63.             ex.printStackTrace();
  64.                }
  65.      }
  66.      public static void dijkstra(int s){
  67.         for(int i=0;i<node;i++){
  68.             color[i]=0;
  69.             parent[i]=-1;
  70.             d[i]=99999;
  71.         }
  72.        
  73.        
  74.         d[s]=0;
  75.         color[s]=1;
  76.         parent[s]=-1;
  77.        
  78.         q.add(d[s]);
  79.        
  80.         while(!q.isEmpty()){
  81.             int u=findIndex(q.poll(),d);
  82.             //System.out.println("u"+u);
  83.             for(int v=0;v<node;v++){
  84.                 if(matrix[u][v]!=0){
  85.                     if(color[v]==0){
  86.                         if((d[u]+matrix[u][v])<d[v]){
  87.                             //System.out.println("v"+v);
  88.                             d[v]=d[u]+matrix[u][v];
  89.                             parent[v]=u;
  90.                            
  91.                         }
  92.                     }
  93.                 }
  94.             }
  95.             color[u]=1;
  96.             queueSort();
  97.            
  98.         }
  99.        
  100.         for(int i=0;i<node;i++){
  101.             if(i!=s){
  102.             System.out.println(d[i]);
  103.             }
  104.            
  105.         }
  106.        
  107.        
  108.      }
  109.      
  110.      public static int findIndex(int value,int [] arr) {
  111.          int index=-1;
  112.          for(int i=0;i<arr.length;i++){
  113.              if(value==arr[i] ){
  114.                  index=i;
  115.              }
  116.          }
  117.         return index;
  118.     }
  119.      
  120.       public static void queueSort(){
  121.        q.clear();
  122.         for(int k=0;k<node;k++){
  123.             if(color[k]==0){
  124.             q.add(d[k]);
  125.             //System.out.println(d[k]);
  126.                
  127.         }
  128.        
  129.        }
  130.           //System.out.println("queue"+q.toString());
  131.      
  132.      
  133.     }
  134.      
  135.  
  136.     /* public static void printPath(int node){
  137.         int x=node;
  138.         int sum=0;
  139.         while(x!=-1 )
  140.         {
  141.             System.out.print(x+"<<-");
  142.             sum+=d[x];
  143.             System.out.println(d[x]);
  144.             x=parent[x];
  145.            
  146.         }
  147.          System.out.println("SUM"+sum);
  148.         System.out.println();
  149.     }*/
  150.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement