Problem:
Score : 500 points

Problem Statement

There are N towns numbered 1 to N and M roads. The i-th road connects Town A_i and Town B_i bidirectionally and has a length of C_i.

Takahashi will travel between these towns by car, passing through these roads. The fuel tank of his car can contain at most L liters of fuel, and one liter of fuel is consumed for each unit distance traveled. When visiting a town while traveling, he can full the tank (or choose not to do so). Travel that results in the tank becoming empty halfway on the road cannot be done.

Process the following Q queries:

The tank is now full. Find the minimum number of times he needs to full his tank while traveling from Town s_i to Town t_i. If Town t_i is unreachable, print -1.

Constraints

All values in input are integers.

2 \leq N \leq 300

0 \leq M \leq \frac{N(N-1)}{2}

1 \leq L \leq 10^9

1 \leq A_i, B_i \leq N

A_i \neq B_i

\left(A_i, B_i\right) \neq \left(A_j, B_j\right) (if i \neq j)

\left(A_i, B_i\right) \neq \left(B_j, A_j\right) (if i \neq j)

1 \leq C_i \leq 10^9

1 \leq Q \leq N\left(N-1\right)

1 \leq s_i, t_i \leq N

s_i \neq t_i

\left(s_i, t_i\right) \neq \left(s_j, t_j\right) (if i \neq j)

Input

Input is given from Standard Input in the following format:

N M L
A_1 B_1 C_1
:
A_M B_M C_M
Q
s_1 t_1
:
s_Q t_Q

Output

Print Q lines.

The i-th line should contain the minimum number of times the tank needs to be fulled while traveling from Town s_i to Town t_i. If Town t_i is unreachable, the line should contain -1 instead.

Sample Input 1

3 2 5
1 2 3
2 3 3
2
3 2
1 3

Sample Output 1

0
1

To travel from Town 3 to Town 2, we can use the second road to reach Town 2 without fueling the tank on the way.

To travel from Town 1 to Town 3, we can first use the first road to get to Town 2, full the tank, and use the second road to reach Town 3.

Sample Input 2

4 0 1
1
2 1

Sample Output 2

-1

There may be no road at all.

Sample Input 3

5 4 4
1 2 2
2 3 2
3 4 3
4 5 2
20
2 1
3 1
4 1
5 1
1 2
3 2
4 2
5 2
1 3
2 3
4 3
5 3
1 4
2 4
3 4
5 4
1 5
2 5
3 5
4 5

Sample Output 3

0
0
1
2
0
0
1
2
0
0
0
1
1
1
0
0
2
2
1
0

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

// =========
#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet};
#[allow(unused_imports)]
use std::process::exit;

#[allow(dead_code)]
const MOD: usize = 1000000007;
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}
#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    // var... 変数の識別子, $t...型を一つよむ
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        //ここで繰り返し
        input_inner!{$iter $($r)*}
    };
}
#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    //
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    // 配列の最後のNestではここで型が指定されてparseされる
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// =========
fn warshall_floyd_next(adjm: &mut Vec<Vec<Option<i64>>>) -> Vec<Vec<Option<usize>>> {
    let n = adjm.len();
    // Optionの行列、行けないならNone, 行けるなら次の点のIndex
    let mut next: Vec<Vec<Option<usize>>> = vec![vec![None; n]; n];
    //今ある辺で初期化
    for i in 0..n {
        for j in 0..n {
            match adjm[i][j] {
                Some(_) => next[i][j] = Some(j),
                None => (),
            }
        }
    }

    // kを経て短くなる辺を全部短くする
    for k in 0..n {
        for i in 0..n {
            for j in 0..n {
                if let (Some(dik), Some(djk)) = (adjm[i][k], adjm[k][j]) {
                    if adjm[i][j].is_none() || adjm[i][j].unwrap() > dik + djk {
                        adjm[i][j] = Some(dik + djk);
                        // 一つの辺でいけるわけでないので、kではだめ
                        // adjm[i][k]がNoneでないならnext[i][k]はNoneでない
                        next[i][j] = next[i][k];
                    }
                }
            }
        }
    }
    next
}

fn warshall_floyd_path(i: usize, j: usize, next: &Vec<Vec<Option<usize>>>) -> Option<Vec<usize>> {
    let mut cur = i;
    let mut res = vec![];
    while cur != j {
        match next[cur][j] {
            Some(id) => {
                cur = id;
                res.push(id);
            }
            None => return None,
        }
    }
    Some(res)
}

fn main() {
    input! {
        n: usize,
        m: usize,
        l: i64,
        edges: [(usize,usize,i64);m],
        q: usize,
        queries: [(usize,usize);q],
    }
    let mut adjm: Vec<Vec<Option<i64>>> = vec![vec![None; n]; n];
    for e in edges {
        adjm[e.0 - 1][e.1 - 1] = Some(e.2);
        adjm[e.1 - 1][e.0 - 1] = Some(e.2);
    }
    // 自分から自分はゼロ for 燃料判定
    for i in 0..n {
        adjm[i][i] = Some(0);
    }
    let next = warshall_floyd_next(&mut adjm);
    'outer: for q in queries {
        match warshall_floyd_path(q.0 - 1, q.1 - 1, &next) {
            Some(p) => {
                let mut fuel = l;
                let mut curv = q.0 - 1;
                let mut nextv = p[0];
                let mut ans = 0;
                for i in 0..p.len() {
                    fuel -= adjm[curv][nextv].unwrap();
                    //次を見る
                    curv = nextv;
                    nextv = p[min(i + 1, p.len() - 1)];
                    // 道でつきたら| 次はどうあがいても無理なら終わり
                    if fuel < 0 || l < adjm[curv][nextv].unwrap() as i64 {
                        println!("{:?}", -1);
                        continue 'outer;
                    } else if fuel < adjm[curv][nextv].unwrap() as i64 {
                        fuel = l;
                        ans += 1;
                    }
                }
                println!("{:?}", ans);
            }
            None => println!("{:?}", -1),
        }
    }
}