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 C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder.Contest.E
{
	class Program
	{
		public static void Main(string[] args)
		{
			var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
			Console.SetOut(sw);

			Scanner cin = new Scanner();

			int n = cin.nextInt();
			int m = cin.nextInt();
			int l = cin.nextInt();
			var a = new List<int>();
			var b = new List<int>();
			var c = new List<int>();
			for (int i = 0; i < m; i++)
			{
				int x;
				x = cin.nextInt();
				a.Add(x);
				x = cin.nextInt();
				b.Add(x);
				x = cin.nextInt();
				c.Add(x);
			}
			int q = cin.nextInt();
			var s = new List<int>();
			var t = new List<int>();
			for (int i = 0; i < q; i++)
			{
				int x = cin.nextInt();
				s.Add(x);
				x = cin.nextInt();
				t.Add(x);
			}

			var ijk = new Dijkstra(n);
			for (int i = 0; i < m; i++)
			{
				ijk.AddPath(a[i] - 1, b[i] - 1, c[i]);
			}
			long xx = 0;
			for (int i = 0; i < q; i++)
			{
				xx = ijk.GetMinCost(s[i] - 1, t[i] - 1, l);
				if (xx >= long.MaxValue / 10000000000)
				{
					Console.WriteLine(-1);
				}
				else
				{
					Console.WriteLine(xx);
				}
			}

			Console.Out.Flush();
		}
	}
	
	/// <summary>
	 /// Get min cost between two points
	 /// </summary>
	public class Dijkstra
	{
		private int maxIndex = -1;
		private const long INF = long.MaxValue;

		private long[,] _edgeArray;

		private const long Cost1 = 10000000000;

		/// <summary>
		/// Dijkstra, get min cost between two points
		/// should not contain negatie cost path
		/// </summary>
		/// <param name="size">max index of vertices</param>
		public Dijkstra(int size)
		{
			maxIndex = size + 1;
			_edgeArray = new long[maxIndex, maxIndex];

			for (int i = 0; i < _edgeArray.GetLength(0); i++)
			{
				for (int j = 0; j < _edgeArray.GetLength(1); j++)
				{
					_edgeArray[i, j] = INF;
					if (i == j) _edgeArray[i, j] = 0;
				}
			}
		}

		// Add a path(no direction) with its cost
		public void AddPath(int s, int t, int cost)
		{
			_edgeArray[s, t] = Math.Min(_edgeArray[s, t], cost);
			_edgeArray[t, s] = _edgeArray[s, t];
		}

		//Get the min cost between s and t
		//return Int32.MaxValue if no path
		public long GetMinCost(int s, int t)
		{
			long[] cost = new long[maxIndex];
			for (int i = 0; i < cost.Length; i++) cost[i] = INF;
			cost[s] = 0;

			var priorityQueue = new PriorityQueue<ComparablePair<long, int>>(maxIndex);
			priorityQueue.Push(new ComparablePair<long, int>(0, s));

			while (priorityQueue.Count() > 0)
			{
				var costDestinationPair = priorityQueue.Pop();
				if (cost[costDestinationPair.Item2] < costDestinationPair.Item1) continue;

				for (int i = 0; i < maxIndex; i++)
				{
					long newCostToi = _edgeArray[costDestinationPair.Item2, i] == INF ? INF :
						costDestinationPair.Item1 + _edgeArray[costDestinationPair.Item2, i];
					if (newCostToi < cost[i])
					{
						cost[i] = newCostToi;
						priorityQueue.Push(new ComparablePair<long, int>(newCostToi, i));
					}
				}
			}

			return cost[t];
		}

		//Get the min cost from s to t
		//return Int32.MaxValue if no path
		public long GetMinCost(int s, int t, int l)
		{
			var cost = new long[maxIndex];
			for (int i = 0; i < cost.Length; i++) cost[i] = INF;
			cost[s] = 0;

			var priorityQueue = new PriorityQueue<ComparablePair<long, int>>(maxIndex);
			priorityQueue.Push(new ComparablePair<long, int>(0, s));

			while (priorityQueue.Count() > 0)
			{
				var costDestinationPair = priorityQueue.Pop();
				if (cost[costDestinationPair.Item2].CompareTo(costDestinationPair.Item1)<0) continue;

				for (int i = 0; i < maxIndex; i++)
				{
					var newCostToi = INF;
					if (_edgeArray[costDestinationPair.Item2, i] != INF)
					{
						newCostToi = costDestinationPair.Item1 + _edgeArray[costDestinationPair.Item2, i];
						if (newCostToi % Cost1 > l)
						{
							newCostToi += Cost1;
							newCostToi /= Cost1;
							newCostToi *= Cost1;
							newCostToi += _edgeArray[costDestinationPair.Item2, i];
						}
					}


					if (newCostToi < cost[i])
					{
						cost[i] = newCostToi;
						priorityQueue.Push(new ComparablePair<long, int>(newCostToi, i));
					}
				}
			}

			return cost[t] / Cost1;
		}
	}

	public class PriorityQueue<T> where T : IComparable
	{
		private IComparer<T> _comparer = null;
		private int _type = 0;

		private T[] _heap;
		private int _sz = 0;

		private int _count = 0;

		/// <summary>
		/// Priority Queue with custom comparer
		/// </summary>
		public PriorityQueue(int maxSize, IComparer<T> comparer)
		{
			_heap = new T[maxSize];
			_comparer = comparer;
		}

		/// <summary>
		/// Priority queue
		/// </summary>
		/// <param name="maxSize">max size</param>
		/// <param name="type">0: asc, 1:desc</param>
		public PriorityQueue(int maxSize, int type = 0)
		{
			_heap = new T[maxSize];
			_type = type;
		}

		private int Compare(T x, T y)
		{
			if (_comparer != null) return _comparer.Compare(x, y);
			return _type == 0 ? x.CompareTo(y) : y.CompareTo(x);
		}

		public void Push(T x)
		{
			_count++;

			//node number
			var i = _sz++;

			while (i > 0)
			{
				//parent node number
				var p = (i - 1) / 2;

				if (Compare(_heap[p], x) <= 0) break;

				_heap[i] = _heap[p];
				i = p;
			}

			_heap[i] = x;
		}

		public T Pop()
		{
			_count--;

			T ret = _heap[0];
			T x = _heap[--_sz];

			int i = 0;
			while (i * 2 + 1 < _sz)
			{
				//children
				int a = i * 2 + 1;
				int b = i * 2 + 2;

				if (b < _sz && Compare(_heap[b], _heap[a]) < 0) a = b;

				if (Compare(_heap[a], x) >= 0) break;

				_heap[i] = _heap[a];
				i = a;
			}

			_heap[i] = x;

			return ret;
		}

		public int Count()
		{
			return _count;
		}

		public T Peek()
		{
			return _heap[0];
		}

		public bool Contains(T x)
		{
			for (int i = 0; i < _sz; i++) if (x.Equals(_heap[i])) return true;
			return false;
		}

		public void Clear()
		{
			while (this.Count() > 0) this.Pop();
		}

		public IEnumerator<T> GetEnumerator()
		{
			var ret = new List<T>();

			while (this.Count() > 0)
			{
				ret.Add(this.Pop());
			}

			foreach (var r in ret)
			{
				this.Push(r);
				yield return r;
			}
		}

		public T[] ToArray()
		{
			T[] array = new T[_sz];
			int i = 0;

			foreach (var r in this)
			{
				array[i++] = r;
			}

			return array;
		}
	}

	public class ComparablePair<T, U> : IComparable where T : IComparable<T> where U : IComparable<U>
	{
		public readonly T Item1;
		public readonly U Item2;

		public int CompareTo(object obj)
		{
			ComparablePair<T, U> castedObj = (ComparablePair<T, U>)obj;
			int x = this.Item1.CompareTo(castedObj.Item1);
			if (x != 0) return x;
			else return this.Item2.CompareTo(castedObj.Item2);
		}

		public ComparablePair(T t, U u)
		{
			Item1 = t;
			Item2 = u;
		}
	}

	class Scanner
	{
		string[] s;
		int i;

		char[] cs = new char[] { ' ' };

		public Scanner()
		{
			s = new string[0];
			i = 0;
		}

		public string next()
		{
			if (i < s.Length) return s[i++];
			string st = Console.ReadLine();
			while (st == "") st = Console.ReadLine();
			s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
			if (s.Length == 0) return next();
			i = 0;
			return s[i++];
		}

		public int nextInt()
		{
			return int.Parse(next());
		}

		public int[] ArrayInt(int N, int add = 0)
		{
			int[] Array = new int[N];
			for (int i = 0; i < N; i++)
			{
				Array[i] = nextInt() + add;
			}
			return Array;
		}

		public long nextLong()
		{
			return long.Parse(next());
		}

		public long[] ArrayLong(int N, long add = 0)
		{
			long[] Array = new long[N];
			for (int i = 0; i < N; i++)
			{
				Array[i] = nextLong() + add;
			}
			return Array;
		}

		public double nextDouble()
		{
			return double.Parse(next());
		}

		public double[] ArrayDouble(int N, double add = 0)
		{
			double[] Array = new double[N];
			for (int i = 0; i < N; i++)
			{
				Array[i] = nextDouble() + add;
			}
			return Array;
		}
	}
}