次短路计数
题目描述
有一张 n \mathtt{n} n点 m \mathtt{m} m边的有向有权图,给定起点和终点 s \mathtt{s} s和 t \mathtt{t} t,求 s → t \mathtt{s \to t} s→t有几条次短路
数据范围
2 ≤ n ≤ 1 0 3 , 1 ≤ m ≤ 1 0 4 , 1 ≤ x , y , s , t ≤ n , 1 ≤ \mathtt{2 \le n \le 10^3 , 1 \le m \le 10^4 , 1 \le x,y,s,t \le n , 1 \le} 2≤n≤103,1≤m≤104,1≤x,y,s,t≤n,1≤ 数据组数 ≤ 1 0 3 \mathtt{ \le 10^3} ≤103
ps:应粉丝私信要求,加上数据范围,字体使用打印机体,加上划分线(如果有更好的建议请提出来)
这道题容易想到最短路,但还要有dp,因为可以转移状态(你不这么认为也行),同时我们要设四个数组,每次迭代求出路径与原路径比较
解法-最短路+DP
首先介绍一个东西叫做分类讨论求解次短
首先我们考虑最短路长度为
d
i
s
1
\mathtt{dis1}
dis1,次短为
d
i
s
2
\mathtt{dis2}
dis2
那么有一个新长度为
x
\mathtt{x}
x
1.
x
<
d
i
s
2
:
\mathtt{x < dis2:}
x<dis2:不变
2.
d
i
s
2
≤
x
<
d
i
s
1
:
d
i
s
2
=
x
\mathtt{dis2 \le x < dis1:dis2=x}
dis2≤x<dis1:dis2=x
3.
d
i
s
21
≤
x
:
d
i
s
2
=
d
i
s
1
,
d
i
s
1
=
x
\mathtt{dis21 \le x:dis2=dis1,dis1=x}
dis21≤x:dis2=dis1,dis1=x
同样原理,只要加一个计数即可
我们用
d
i
s
x
,
y
,
0
/
1
\mathtt{dis_{x,y,0/1}}
disx,y,0/1表示
x
→
y
\mathtt{x \to y}
x→y的最短
/
\mathtt{/}
/次短的长度
c
n
t
x
,
y
,
0
/
1
\mathtt{cnt_{x,y,0/1}}
cntx,y,0/1表示
x
→
y
\mathtt{x \to y}
x→y的最短
/
\mathtt{/}
/次短的数量
其中求最短路可以用 d i j s t r a \mathtt{dijstra} dijstra!