Guest User

SPATH

a guest
Mar 12th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5.  
  6. import static java.lang.Integer.parseInt;
  7.  
  8. /**
  9.  * Created by arpit on 11/12/16.
  10.  */
  11.  
  12. class SHPATH {
  13.  
  14.     static List<Pair> adj[] = new List[10002];
  15.     static Map<String, Integer> cityMap = new HashMap<>();
  16.     static int d[] = new int[10002];
  17.     static boolean vis[] = new boolean[10002];
  18.  
  19.     public static void main(String[] args) throws IOException {
  20.  
  21.         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  22.         int n, s, p, r;
  23.         String s1[], cityName;
  24.         s = parseInt(br.readLine());
  25.         while (s-- > 0) {
  26.             n = parseInt(br.readLine());
  27.             for (int i = 1; i <= n; i++) {
  28.                 adj[i] = new ArrayList<>();
  29.             }
  30.             for (int i = 1; i <= n; i++) {
  31.                 cityName = br.readLine();
  32.                 cityMap.put(cityName, i);
  33.                 //p: no. of neighbours
  34.                 p = parseInt(br.readLine());
  35.                 for (int j = 0; j < p; j++) {
  36.                     s1 = br.readLine().split("\\s");
  37.                     int v = parseInt(s1[0]);
  38.                     int w = parseInt(s1[1]);
  39.                     adj[i].add(new Pair(v, w));
  40.                 }
  41.             }
  42.             r = parseInt(br.readLine());
  43.             for (int j = 0; j < r; j++) {
  44.                 s1 = br.readLine().split("\\s");
  45.                 System.out.println(getShortestPath(adj,n, cityMap.get(s1[0]), cityMap.get(s1[1])));
  46.             }
  47.         }
  48.     }
  49.  
  50.     private static int getShortestPath(List<Pair>[] adj,int n, int source,int destination) {
  51.  
  52.         PriorityQueue<Pair>pq=new PriorityQueue<>();
  53.  
  54.         Arrays.fill(d, 1, n + 1, Integer.MAX_VALUE);
  55.         Arrays.fill(vis, 1, n + 1, false);
  56.  
  57.         pq.add(new Pair(source,0));
  58.         d[source]=0;
  59.         while (!pq.isEmpty()){
  60.             Pair p=pq.poll();
  61.             int u=p.v;
  62.             if (u == destination) {
  63.                 break;
  64.             }
  65.             vis[u]=true;
  66.             for (int i = 0; i < adj[u].size(); i++) {
  67.                 int v=adj[u].get(i).v;
  68.                 int w=adj[u].get(i).w;
  69.                 if (!vis[v]){
  70.                     d[v]=Integer.min(d[v],d[u]+w);
  71.                     pq.add(new Pair(v,d[v]));
  72.                 }
  73.             }
  74.         }
  75.         return d[destination];
  76.     }
  77.  
  78.     static class Pair implements Comparable<Pair>{
  79.  
  80.         int v,w;
  81.  
  82.         public Pair(int v, int w) {
  83.             this.v = v;
  84.             this.w = w;
  85.         }
  86.  
  87.         @Override
  88.         public int compareTo(Pair pair) {
  89.             return ((Integer)this.w).compareTo(pair.w);
  90.         }
  91.  
  92.         @Override
  93.         public String toString() {
  94.             return "Pair{" +
  95.                     "v=" + v +
  96.                     ", w=" + w +
  97.                     '}';
  98.         }
  99.     }
  100. }
Add Comment
Please, Sign In to add comment