Weird Graph Query

4.4

8 votes
Advanced Data Structures, Data Structures, Segment Trees
Problem

We are given an connected undirected weighted graph of N nodes and M edges. We are then given Q queries where in each query we are given integers U,L,R,K. For every query we have to report the smallest integer C such that there are atleast K nodes x such that LxR and shortest distance from x to U is not more than C

Constraints : 

N1000

M5000

Weight of edge 10000

Q2105

1a,b,U,L,RN

KRL+1

Input: 

The first line contains two integers N,M. The next M lines contains three space separated integers a,b,c meaning that there is an edge between nodes a and b of weight c. The next line contains an integer Q. Q lines follow, each with four integers U,L,R,K with the meaning as given in problem. 

Output : 

Q lines should be printed and the ith line should contain the output for the ith query. 

Sample Input
10 15
9 10 2
6 10 2
4 9 1
3 4 2
7 6 1
2 9 1
1 7 1
8 9 2
5 4 1
7 4 2
3 8 1
9 9 2
3 6 1
5 6 1
6 8 1
5
4 4 9 4
3 1 5 3
3 8 8 1
3 7 8 2
7 5 9 3
Sample Output
2
2
1
2
2
Time Limit: 5
Memory Limit: 256
Source Limit:
Editor Image

?