Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class scorify {
- public static void main (String[] args) {
- Scanner in = new Scanner(System.in);
- int T = in.nextInt();
- while(T-->0) {
- int V = in.nextInt();
- int E = in.nextInt();
- int K = in.nextInt();
- Vector<Pair>[] adjList = new Vector[V];
- for(int i = 0; i < V; ++i) {
- adjList[i] = new Vector<Pair>();
- }
- for(int i = 0; i < E; ++i) {
- int u = in.nextInt();
- int v = in.nextInt();
- int w = in.nextInt();
- adjList[u-1].add(new Pair(v-1, w));
- adjList[v-1].add(new Pair(u-1, w));
- }
- // DIJKSTRA
- int S = in.nextInt() - 1;
- int D = in.nextInt() - 1;
- int[] dist = new int[V];
- Arrays.fill(dist, Integer.MAX_VALUE);
- dist[D] = 0;
- PriorityQueue<State> pq = new PriorityQueue();
- pq.offer(new State(D, 0, 0));
- while(!pq.isEmpty()){
- State st = pq.poll();
- int u = st.u;
- int dd = st.d;
- int str = st.s;
- if(str > K) continue;
- if(dist[u] < dd) continue;
- dist[u] = Math.min(dist[u], dd);
- for(Pair pp: adjList[u]) {
- pq.offer(new State(pp.f, dd + pp.s, str + 1));
- }
- }
- if(dist[S] == Integer.MAX_VALUE)
- System.out.println(-1);
- else
- System.out.println(dist[S]);
- }
- }
- }
- class Pair{
- int f, s;
- public Pair(int f, int s) {
- this.f = f;
- this.s = s;
- }
- }
- class State implements Comparable<State> {
- int u, d, s;
- public State(int u, int d, int s) {
- this.u = u;
- this.d = d;
- this.s = s;
- }
- @Override
- public int compareTo(State st) {
- return this.d - st.d;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement