#abc218f. F - Blocked Roads

F - Blocked Roads

Score : 500500 points

问题描述

你得到一个有向图,包含 NN 个顶点和 MM 条边。顶点编号从 11NN,边的编号从 11MM。第 ii 条边 (1iM)(1 \leq i \leq M) 从顶点 sis_i 指向顶点 tit_i,且边长为 11

对于每个 ii (1iM)(1 \leq i \leq M),在除第 ii 条边之外的所有边均可通行的情况下,找出从顶点 11 到顶点 NN 的最短距离。若此时顶点 NN 从顶点 11 不可达,则输出 -1

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a directed graph with NN vertices and MM edges. The vertices are numbered 11 through NN, and the edges are numbered 11 through MM. Edge ii (1iM)(1 \leq i \leq M) goes from Vertex sis_i to Vertex tit_i and has a length of 11.

For each ii (1iM)(1 \leq i \leq M), find the shortest distance from Vertex 11 to Vertex NN when all edges except Edge ii are passable, or print -1 if Vertex NN is unreachable from Vertex 11.

Constraints

  • 2N4002 \leq N \leq 400
  • 1MN(N1)1 \leq M \leq N(N-1)
  • 1si,tiN1 \leq s_i,t_i \leq N
  • sitis_i \neq t_i
  • (si,ti)(sj,tj)(s_i,t_i) \neq (s_j,t_j) (ij)(i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

s1s_1 t1t_1

s2s_2 t2t_2

\vdots

sMs_M tMt_M

Output

Print MM lines.

The ii-th line should contain the shortest distance from Vertex 11 to Vertex NN when all edges except Edge ii are passable, or -1 if Vertex NN is unreachable from Vertex 11.

Sample Input 1

3 3
1 2
1 3
2 3

Sample Output 1

1
2
1

Sample Input 2

4 4
1 2
2 3
2 4
3 4

Sample Output 2

-1
2
3
2

Vertex NN is unreachable from Vertex 11 when all edges except Edge 11 are passable, so the corresponding line contains -1.

Sample Input 3

5 10
1 2
1 4
1 5
2 1
2 3
3 1
3 2
3 5
4 2
4 3

Sample Output 3

1
1
3
1
1
1
1
1
1
1

Sample Input 4

4 1
1 2

Sample Output 4

-1

update @ 2024/3/10 09:39:19