|
|
|
@ -119,13 +119,15 @@ $dist[S][0]$=$0$,$cnt[S][0]$=$1$
|
|
|
|
|
```cpp {.line-numbers}
|
|
|
|
|
#include <bits/stdc++.h>
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
const int N = 1010;
|
|
|
|
|
const int M = 10010;
|
|
|
|
|
int n, m;
|
|
|
|
|
|
|
|
|
|
int dist[N][2];
|
|
|
|
|
int cnt[N][2];
|
|
|
|
|
#define x first
|
|
|
|
|
#define y second
|
|
|
|
|
|
|
|
|
|
const int N = 1e3 + 13;
|
|
|
|
|
const int M = 1e6 + 10;
|
|
|
|
|
int n, m, u, v, s, f;
|
|
|
|
|
// 将最短路扩展为二维,含义:最短路与次短路
|
|
|
|
|
// dis:路径长度,cnt:路线数量,st:是否已经出队列
|
|
|
|
|
int dis[N][2], cnt[N][2];
|
|
|
|
|
bool st[N][2];
|
|
|
|
|
|
|
|
|
|
// 链式前向星
|
|
|
|
@ -134,53 +136,61 @@ void add(int a, int b, int c = 0) {
|
|
|
|
|
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 本题需要一个三个属性的对象:最短距离d,最短、次短k,id:节点号
|
|
|
|
|
struct Node {
|
|
|
|
|
int d, k, id;
|
|
|
|
|
// 小顶堆需要重载大于号,大顶堆需要重载小于号
|
|
|
|
|
bool const operator>(Node b) const {
|
|
|
|
|
return d > b.d;
|
|
|
|
|
// u: 节点号
|
|
|
|
|
// d:目前结点v的路径长度
|
|
|
|
|
// k:是最短路0还是次短路1
|
|
|
|
|
int u, d, k;
|
|
|
|
|
const bool operator<(Node x) const {
|
|
|
|
|
return d > x.d;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void dijkstra(int S) {
|
|
|
|
|
memset(dist, 0x3f, sizeof dist);
|
|
|
|
|
memset(st, false, sizeof st);
|
|
|
|
|
memset(cnt, 0, sizeof cnt);
|
|
|
|
|
priority_queue<Node, vector<Node>, greater<>> pq; // 小顶堆
|
|
|
|
|
dist[S][0] = 0;
|
|
|
|
|
cnt[S][0] = 1;
|
|
|
|
|
pq.push({0, 0, S});
|
|
|
|
|
|
|
|
|
|
while (pq.size()) {
|
|
|
|
|
auto t = pq.top();
|
|
|
|
|
pq.pop();
|
|
|
|
|
int u = t.id;
|
|
|
|
|
int k = t.k;
|
|
|
|
|
|
|
|
|
|
if (st[u][k]) continue;
|
|
|
|
|
void dijkrsta() {
|
|
|
|
|
priority_queue<Node> q; // 默认是大顶堆,通过定义结构体小于号,实现小顶堆。比如:认证的d值更大,谁就更小!
|
|
|
|
|
memset(dis, 0x3f, sizeof dis); // 清空最小距离与次小距离数组
|
|
|
|
|
memset(cnt, 0, sizeof cnt); // 清空最小距离路线个数与次小距离路线个数数组
|
|
|
|
|
memset(st, 0, sizeof st); // 清空是否出队过数组
|
|
|
|
|
|
|
|
|
|
cnt[s][0] = 1; // 起点s,0:最短路,1:有一条
|
|
|
|
|
cnt[s][1] = 0; // 次短路,路线数为0
|
|
|
|
|
|
|
|
|
|
dis[s][0] = 0; // 最短路从s出发到s的距离是0
|
|
|
|
|
dis[s][1] = 0; // 次短路从s出发到s的距离是0
|
|
|
|
|
|
|
|
|
|
q.push({s, 0, 0}); // 入队列
|
|
|
|
|
|
|
|
|
|
while (q.size()) {
|
|
|
|
|
Node x = q.top();
|
|
|
|
|
q.pop();
|
|
|
|
|
|
|
|
|
|
int u = x.u, k = x.k; // u:节点号,k:是最短路还是次短路,d:路径长度(这个主要用于堆中排序,不用于实战,实战中可以使用dis[u][k])
|
|
|
|
|
|
|
|
|
|
if (st[u][k]) continue; // ① 和dijkstra标准版本一样的,只不过多了一个维度
|
|
|
|
|
st[u][k] = true;
|
|
|
|
|
|
|
|
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
|
|
|
int v = e[i];
|
|
|
|
|
int d = dist[u][k] + w[i];
|
|
|
|
|
|
|
|
|
|
if (dist[v][0] > d) { // 比最短路还要短
|
|
|
|
|
dist[v][1] = dist[v][0]; // 最短降为次短
|
|
|
|
|
cnt[v][1] = cnt[v][0]; // 次短路数量被更新
|
|
|
|
|
pq.push({dist[v][1], 1, v}); // 次短被更新,次短入队列
|
|
|
|
|
|
|
|
|
|
dist[v][0] = d; // 替换最短路
|
|
|
|
|
cnt[v][0] = cnt[u][k]; // 替换最短路数量
|
|
|
|
|
pq.push({dist[v][0], 0, v}); // 最短路入队列
|
|
|
|
|
} else if (dist[v][0] == d) // 增加最短路的数量
|
|
|
|
|
cnt[v][0] += cnt[u][k];
|
|
|
|
|
else if (dist[v][1] > d) { // 替换次短路
|
|
|
|
|
dist[v][1] = d;
|
|
|
|
|
cnt[v][1] = cnt[u][k];
|
|
|
|
|
pq.push({dist[v][1], 1, v}); // 次短路入队列
|
|
|
|
|
} else if (dist[v][1] == d)
|
|
|
|
|
cnt[v][1] += cnt[u][k];
|
|
|
|
|
int j = e[i];
|
|
|
|
|
int dj = dis[u][k] + w[i]; // 原长度+到节点j的边长
|
|
|
|
|
|
|
|
|
|
if (dj == dis[j][0]) // 与到j的最短长度相等,则更新路径数量
|
|
|
|
|
cnt[j][0] += cnt[u][k];
|
|
|
|
|
else if (dj < dis[j][0]) { // 找到更小的路线,需要更新
|
|
|
|
|
dis[j][1] = dis[j][0]; // 次短距离被最短距离覆盖
|
|
|
|
|
cnt[j][1] = cnt[j][0]; // 次短个数被最短个数覆盖
|
|
|
|
|
|
|
|
|
|
dis[j][0] = dj; // 更新最短距离
|
|
|
|
|
cnt[j][0] = cnt[u][k]; // 更新最短个数
|
|
|
|
|
|
|
|
|
|
q.push({j, dis[j][1], 1}); // ②
|
|
|
|
|
q.push({j, dis[j][0], 0});
|
|
|
|
|
} else if (dj == dis[j][1]) // 如果等于次短
|
|
|
|
|
cnt[j][1] += cnt[u][k]; // 更新次短的方案数,累加
|
|
|
|
|
else if (dj < dis[j][1]) { // 如果大于最短,小于次短,两者中间
|
|
|
|
|
dis[j][1] = dj; // 更新次短距离
|
|
|
|
|
cnt[j][1] = cnt[u][k]; // 更新次短方案数
|
|
|
|
|
q.push({j, dis[j][1], 1}); // 次短入队列
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -188,22 +198,21 @@ int main() {
|
|
|
|
|
int T;
|
|
|
|
|
scanf("%d", &T);
|
|
|
|
|
while (T--) {
|
|
|
|
|
scanf("%d %d", &n, &m);
|
|
|
|
|
memset(h, -1, sizeof h);
|
|
|
|
|
idx = 0;
|
|
|
|
|
scanf("%d %d", &n, &m);
|
|
|
|
|
while (m--) {
|
|
|
|
|
int a, b, c;
|
|
|
|
|
scanf("%d %d %d", &a, &b, &c);
|
|
|
|
|
add(a, b, c);
|
|
|
|
|
}
|
|
|
|
|
int S, F;
|
|
|
|
|
scanf("%d %d", &S, &F);
|
|
|
|
|
dijkstra(S);
|
|
|
|
|
int ans = cnt[F][0]; // 最短路
|
|
|
|
|
// 在正常处理完最短路和次短路后,在最后的逻辑中,增加本题的中特殊要求部分
|
|
|
|
|
if (dist[F][0] == dist[F][1] - 1) ans += cnt[F][1];
|
|
|
|
|
printf("%d\n", ans);
|
|
|
|
|
// 起点和终点
|
|
|
|
|
scanf("%d %d", &s, &f);
|
|
|
|
|
// 计算最短路
|
|
|
|
|
dijkrsta();
|
|
|
|
|
// 输出
|
|
|
|
|
printf("%d\n", cnt[f][0] + (dis[f][1] == dis[f][0] + 1 ? cnt[f][1] : 0));
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|