QOJ.ac

QOJ

Límite de tiempo: 2.0 s Límite de memoria: 512 MB Puntuación total: 100 Hackeable ✓

#9903. Shortest Path

Estadísticas

Given a directed graph with $n$ vertices, labeled $1$ to $n$, there is an edge from $i$ to $j$ for any two distinct vertices $i \neq j$, with a weight of $a_{i,j}$.

Over the next $q$ days, one node $p_i$ will be closed each day. For each day, you need to find the shortest path length from $s_i$ to $t_i$ without passing through $p_i$.

Input

The first line contains two positive integers $n$ and $q$ ($3 \leq n \leq 300, 1 \leq q \leq 5\times 10^5$), representing the number of vertices in the directed graph and the number of queries, respectively.

The next $n$ lines each contain $n$ integers, where the $j$-th integer in the $i$-th line represents $a_{i, j}$ ($0 \leq a_{i, j} \leq 10^{14}, a_{i, i}=0$).

The following $q$ lines each contain three integers $s_i, t_i, p_i$ ($1 \leq s_i, t_i, p_i \leq n$, where $s_i, t_i, p_i$ are distinct), representing the query for the shortest path length from $s_i$ to $t_i$ without passing through $p_i$.

Output

Output $q$ lines, each containing an integer representing the answer.

Examples

Input 1

3 3
0 7 8
14 0 5
8 16 0
3 2 1
1 3 2
2 1 3

Output 1

16
8
14

Input 2

5 8
0 15 8 7 8
8 0 8 6 8
8 7 0 14 7
5 7 6 0 14
12 8 7 6 0
4 3 5
4 5 1
5 1 4
4 5 3
1 2 4
2 3 5
3 4 2
3 4 5

Output 2

6
13
12
13
15
8
13
13

Note

For $30\%$ of the test cases: $n, q \leq 100$.

For $50\%$ of the test cases: $q \leq 1000$.

For all test cases: $1 \leq s_i, t_i, p_i \leq n \leq 300, 1 \leq q \leq 5\times 10^5, 0 \leq a_{i, j} \leq 10^{14}, \forall 1 \leq i \leq n: a_{i,i}=0$.

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.