QOJ.ac

QOJ

実行時間制限: 4 s メモリ制限: 1024 MB 満点: 100 ハック可能 ✓

#18592. Shortest Distance and Queries

統計

You are given a simple undirected weighted connected graph with $N$ vertices and $M$ edges.

Process $Q$ queries to find the shortest distance between two vertices $s$ and $e$.

The first line contains the number of vertices $N$ and the number of edges $M$, separated by a space. $(1 \le N \le 10^{5}; N-1 \le M \le N+50)$

From the second line, $M$ lines follow, each containing two vertices $u$ and $v$ connected by an edge, and the weight of the edge $w$, separated by spaces. $(1 \le u, v \le N; u \neq v; 1 \le w \le 10^{9})$

The $(M+2)$-th line contains the number of queries $Q$. $(1 \le Q \le 10^{5})$

From the $(M+3)$-th line, $Q$ lines follow, each containing two vertices $s$ and $e$ for which the shortest distance must be found, separated by a space. $(1 \le s, e \le N)$

For each of the $Q$ queries, output the shortest distance from vertex $s$ to vertex $e$ on a new line.

Examples

Input 1

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

Output 1

2
4

Input 2

6 8
1 2 1
3 1 2
2 3 2
5 2 1
3 5 4
5 4 5
2 6 3
4 2 3
4
1 6
4 1
5 5
6 3

Output 2

4
4
0
5

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.