Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class scorify {
  4.     public static void main (String[] args) {
  5.         Scanner in = new Scanner(System.in);
  6.         int T = in.nextInt();
  7.        
  8.         while(T-->0) {
  9.             int V = in.nextInt();
  10.             int E = in.nextInt();
  11.             int K = in.nextInt();
  12.            
  13.             Vector<Pair>[] adjList = new Vector[V];
  14.             for(int i = 0; i < V; ++i) {
  15.                 adjList[i] = new Vector<Pair>();
  16.             }
  17.            
  18.             for(int i = 0; i < E; ++i) {
  19.                 int u = in.nextInt();
  20.                 int v = in.nextInt();
  21.                 int w = in.nextInt();
  22.                
  23.                 adjList[u-1].add(new Pair(v-1, w));
  24.                 adjList[v-1].add(new Pair(u-1, w));
  25.             }
  26.            
  27.            
  28.             // DIJKSTRA
  29.             int S = in.nextInt() - 1;
  30.             int D = in.nextInt() - 1;
  31.            
  32.             int[] dist = new int[V];
  33.             Arrays.fill(dist, Integer.MAX_VALUE);
  34.             dist[D] = 0;
  35.             PriorityQueue<State> pq = new PriorityQueue();
  36.             pq.offer(new State(D, 0, 0));
  37.             while(!pq.isEmpty()){
  38.                 State st = pq.poll();
  39.                 int u = st.u;
  40.                 int dd = st.d;
  41.                 int str = st.s;
  42.                 if(str > K) continue;
  43.                
  44.                 if(dist[u] < dd) continue;
  45.                 dist[u] = Math.min(dist[u], dd);
  46.                
  47.                 for(Pair pp: adjList[u]) {
  48.                     pq.offer(new State(pp.f, dd + pp.s, str + 1));
  49.                 }
  50.             }
  51.            
  52.             if(dist[S] == Integer.MAX_VALUE)
  53.                 System.out.println(-1);
  54.             else
  55.                 System.out.println(dist[S]);
  56.         }
  57.     }
  58. }
  59.  
  60. class Pair{
  61.     int f, s;
  62.    
  63.     public Pair(int f, int s) {
  64.         this.f = f;
  65.         this.s = s;
  66.     }
  67. }
  68.  
  69. class State implements Comparable<State> {
  70.     int u, d, s;
  71.    
  72.     public State(int u, int d, int s) {
  73.         this.u = u;
  74.         this.d = d;
  75.         this.s = s;
  76.     }
  77.    
  78.     @Override
  79.     public int compareTo(State st) {
  80.         return this.d - st.d;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement