Mock CCC '19 Contest 1 S5 - Pusheen Eats Even More Tuna Sashimi and Tuna Nigiri

View as PDF

Submit solution


Points: 20
Time limit: 0.5s
Memory limit: 256M

Problem types

Pusheen has been dreaming about tuna sashimi! She has decided that she needs to eat more tuna in her life, so she decides to go on a tuna tour to eat tuna sashimi and tuna nigiri!

Pusheen has made a note of N restaurants serving tuna sashimi and tuna nigiri and has made plans to eat at many of them! She has Q trips planned - on trip i, she will start by eating lunch at restaurant si and then walk over to restaurant ei for dinner.

The N restaurants are connected by M bidirectional roads that Pusheen can walk along in either direction. Pusheen is excited to eat as much tuna sashimi and tuna nigiri as possible, so she wants to know the length of the shortest path between each of these restaurants. Pusheen will not need her helicopter for this, as it is guaranteed that she can walk from any restaurant to any other restaurant along these roads.

Constraints

2N5104

N1MN+200

1ai,biN, aibi

There will be at most one road between two restaurants.

It is possible to reach any restaurant from any other restaurant via the given roads.

1wi103

1Q5104

1si,eiN, siei

Input Specification

The first line contains two space-separated positive integers, N and M.

M lines follow, each containing three space-separated positive integers, ai, bi, and wi, indicating a bidirectional road of length wi between restaurants ai and bi.

A single line follows with a single positive integer, Q.

Q lines follow, each containing two space-separated positive integers, si and ei, indicating that Pusheen's ith trip will be between restaurants si and ei.

Output Specification

Output Q lines. On the ith line, compute the length of the shortest path for Pusheen's ith trip.

Sample Input

Copy
3 3
1 2 1
2 3 2
1 3 4
2
1 2
1 3

Sample Output

Copy
1
3

Comments

There are no comments at the moment.