求次短路的思路都相同,都是把值丢到优先队列里,每次取出最小值将其邻点更新。
如果能更新最短距离的话,肯定更新最短距离,换下来的最短距离递补成次短距离;不能更新最短距离的话,再看能否更新次短距离。
而严格次短和非严格次短的区别就在于:最短距离和次短距离能否更新成相同值。
一、非严格次短路
模型
次短路的长度可以和最短路长度相同,问次短路长度为多少?
思路
定义两个 dist数组
,用于存放最短距离和次短距离。
从优先队列里面取出一点 dis
,遍历所有邻点 tx
:
- 如果
dis + w
比tx
的最短距离小,那就把tx
的次短距离更新为最短距离,最短距离更新为dis + w
。 - 否则如果
dis + w
比tx
的次短距离小,那就把tx
的次短距离更新为dis + w
。
(注意这个地方,当dis + w
和最短距离相等时,次短距离更新之后和最短距离相等了)
void dij()
{
mem(dist1, 0x3f);
mem(dist2, 0x3f);
dist1[1] = 0; //这里只把最短距离初始化,次短距离不要动
priority_queue<PII, vector<PII>, greater<PII> > que;
que.push({0, 1});
while(que.size())
{
int x = que.top().se, dis = que.top().fi;
que.pop();
if(dis > dist2[x]) continue; //相当于vis数组
for(auto it : e[x])
{
int tx = it.fi, w = it.se;
if(dist1[tx] > dis + w)
{
dist2[tx] = dist1[tx];
dist1[tx] = dis + w;
que.push({dist1[tx], tx});
}
else if(dist2[tx] > dis + w){
dist2[tx] = dis + w;
que.push({dist2[tx], tx});
}
}
}
}
二、严格次短路
模型
次短路的长度要严格大于最短路长度,求次短路长度?
思路
这里和上面 非严格次短路 不同的是,在更新次短路的时候,要保证更新之后不能和最短路相同。
也就是说,如果更新值和最短路不相同时,才将次短路更新。
void dij()
{
mem(dist1, 0x3f);
mem(dist2, 0x3f);
dist1[1] = 0;
priority_queue<PII, vector<PII>, greater<PII> > que;
que.push({0, 1});
while(que.size())
{
int x = que.top().se, dis = que.top().fi;
que.pop();
if(dis > dist2[x]) continue;
for(auto it : e[x])
{
int tx = it.fi, w = it.se;
if(dist1[tx] > dis + w)
{
dist2[tx] = dist1[tx];
dist1[tx] = dis + w;
que.push({dist1[tx], tx});
}
else if(dist1[tx] != dis + w && dist2[tx] > dis + w){
dist2[tx] = dis + w;
que.push({dist2[tx], tx});
}
}
}
}
例题:P2865 [USACO06NOV]Roadblocks G
三、严格次短路计数
路径计数问题,需要加上 cnt[]数组。
cnt1[], cnt2[]
分别表示到达一个点的最短路个数和次短路个数。
初始化到达起点的最短路长度为0,最短路个数为1。
每更新到一个点 x,距离 dis,遍历邻接点 tx:
- 如果
dis + w
小于dist1[tx]
,说明能更新这个点的最短距离,那么次短路递补,次短路长度更新为原最短路长度,次短路个数更新为原最短路个数;最短路长度更新,最短路个数重置为1。 - 否则如果
dis + w
等于dist1[tx]
,说明有新的最短路径能够到达该点,那么最短路径的个数就要加上点 x 的这个路径的个数。
(但是不知道这个是点 x 的最短路径个数还是次短路径个数,所以就需要在优先队列中标明是最短路还是次短路。) - 否则如果 dis + w 小于 dist2[tx],那么更新次短路,次短路个数重置为1。
- 否则如果 dis + w 等于 dist2[tx],次短路路径数累加上当前 x 的对应路径数。
所以额外注意的就是,要在优先队列中表明队里的每个元素是更新最短路径进来的还是次短路径进来的,方便用于相邻点路径数的累加。
例题:观光
Code
#include<bits/stdc++.h>
using namespace std;
#define Ios ios::sync_with_stdio(false),cin.tie(0)
#define mem(a,b) memset(a,b,sizeof a)
#define int long long
#define PII pair<int,int>
#define pb push_back
#define fi first
#define se second
#define endl '\n'
map<int,int> mp;
const int N = 2010, mod = 1e9+7;
int T, n, m, k;
int a[N];
vector<PII> e[N];
int dist[N][3], cnt[N][3];
int f[N][3];
int st, en;
struct node{
int x, type, dis;
friend bool operator < (node a, node b){
return a.dis > b.dis;
}
};
void dij()
{
mem(dist, 0x3f);
mem(cnt, 0);
mem(f, 0);
dist[st][1] = 0;
cnt[st][1] = 1;
priority_queue<node> que;
que.push({st, 1, 0});
while(que.size())
{
int x = que.top().x, type = que.top().type;
que.pop();
if(f[x][type]) continue;
f[x][type] = 1;
for(auto it:e[x])
{
int tx = it.fi, w = it.se;
if(dist[tx][1] > dist[x][type]+w)
{
dist[tx][2] = dist[tx][1]; cnt[tx][2] = cnt[tx][1];
que.push({tx, 2, dist[tx][2]});
dist[tx][1] = dist[x][type]+w; cnt[tx][1] = cnt[x][type];
que.push({tx, 1, dist[tx][1]});
}
else if(dist[tx][1] == dist[x][type]+w) cnt[tx][1]+=cnt[x][type];
else if(dist[tx][2] > dist[x][type]+w)
{
dist[tx][2] = dist[x][type]+w, cnt[tx][2] = cnt[x][type];
que.push({tx, 2, dist[tx][2]});
}
else if(dist[tx][2] == dist[x][type]+w) cnt[tx][2]+=cnt[x][type];
}
}
}
signed main(){
Ios;
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=1;i<=n;i++) e[i].clear();
while(m--)
{
int x, y, z;cin>>x>>y>>z;
e[x].pb({y, z});
}
cin>>st>>en;
dij();
if(dist[en][2]==dist[en][1]+1) cout<<cnt[en][1]+cnt[en][2]<<endl;
else cout<<cnt[en][1]<<endl;
}
return 0;
}