diff --git a/TangDou/AcWing/MiniPath/POJ3464.cpp b/TangDou/AcWing/MiniPath/POJ3464.cpp deleted file mode 100644 index fb9b641..0000000 --- a/TangDou/AcWing/MiniPath/POJ3464.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include -#include -#include -#include -using namespace std; -#define x first -#define y second - -const int N = 1e3 + 13; -const int M = 1e6 + 10; -int n, m, u, v, s, f; -int dist[N][2], cnt[N][2]; -bool st[N][2]; - -//链式前向星 -int e[M], h[N], idx, w[M], ne[M]; -void add(int a, int b, int c = 0) { - e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++; -} - -struct Node { - // u: 节点号 - // d:目前结点v的路径长度 - // k:是最短路0还是次短路1 - int u, d, k; - // POJ中结构体,没有构造函数,直接报编译错误 - Node(int u, int d, int k) { - this->u = u, this->d = d, this->k = k; - } - const bool operator<(Node x) const { - return d > x.d; - } -}; - -void dijkrsta() { - priority_queue q; //通过定义结构体小于号,实现小顶堆 - memset(dist, 0x3f, sizeof(dist)); //清空最小距离与次小距离数组 - memset(cnt, 0, sizeof(cnt)); //清空最小距离路线个数与次小距离路线个数数组 - memset(st, 0, sizeof(st)); //清空是否出队过数组 - - cnt[s][0] = 1; //起点s,0:最短路,1:有一条 - cnt[s][1] = 0; //次短路,路线数为0 - - dist[s][0] = 0; //最短路从s出发到s的距离是0 - dist[s][1] = 0; //次短路从s出发到s的距离是0 - - q.push(Node(s, 0, 0)); //入队列 - - while (q.size()) { - Node x = q.top(); - q.pop(); - - int u = x.u, k = x.k, d = x.d; - - if (st[u][k]) continue; //① - st[u][k] = true; - - for (int i = h[u]; ~i; i = ne[i]) { - int j = e[i]; - int dj = d + w[i]; //原长度+到节点j的边长 - - if (dj == dist[j][0]) //与到j的最短长度相等,则更新路径数量 - cnt[j][0] += cnt[u][k]; - else if (dj < dist[j][0]) { //找到更小的路线,需要更新 - dist[j][1] = dist[j][0]; //次短距离被最短距离覆盖 - cnt[j][1] = cnt[j][0]; //次短个数被最短个数覆盖 - - dist[j][0] = dj; //更新最短距离 - cnt[j][0] = cnt[u][k]; //更新最短个数 - - q.push(Node(j, dist[j][1], 1)); //② - q.push(Node(j, dist[j][0], 0)); - } else if (dj == dist[j][1]) //如果等于次短 - cnt[j][1] += cnt[u][k]; //更新次短的方案数,累加 - else if (dj < dist[j][1]) { //如果大于最短,小于次短,两者中间 - dist[j][1] = dj; //更新次短距离 - cnt[j][1] = cnt[u][k]; //更新次短方案数 - q.push(Node(j, dist[j][1], 1)); //次短入队列 - } - } - } -} -int main() { - int T; - scanf("%d", &T); - while (T--) { - memset(h, -1, sizeof h); - scanf("%d %d", &n, &m); - while (m--) { - int a, b, c; - scanf("%d %d %d", &a, &b, &c); - add(a, b, c); - } - //起点和终点 - scanf("%d %d", &s, &f); - //计算最短路 - dijkrsta(); - //输出 - printf("%d\n", cnt[f][0] + (dist[f][1] == dist[f][0] + 1 ? cnt[f][1] : 0)); - } - return 0; -} diff --git a/TangDou/AcWing_TiGao/T3/MiniPathExtend/1134.md b/TangDou/AcWing_TiGao/T3/MiniPathExtend/1134.md index 9200b44..dc7ee6a 100644 --- a/TangDou/AcWing_TiGao/T3/MiniPathExtend/1134.md +++ b/TangDou/AcWing_TiGao/T3/MiniPathExtend/1134.md @@ -203,7 +203,7 @@ int main() { **分析** 只需要计算出最短路的条数和距离、次短路的距离和条数,最后判断最短路和次短路的关系即可,在$dijkstra$求最短路的基础上,**加一维** 保存从起点到该点的 **最短路** 和 **次短路**,**同时记录相应的数量** -如果当前点的最短路或次短路更新了,那么这个点可能松弛其他点,加入优先队列;如果等于最短路或次短路,相应的数量就加 +如果当前点的最短路或次短路更新了,那么这个点可能松弛其他点,加入优先队列;如果等于最短路或次短路,相应的数量就加。 关于代码中①②的自我解释: @@ -224,10 +224,12 @@ using namespace std; const int N = 1e3 + 13; const int M = 1e6 + 10; int n, m, u, v, s, f; -int dist[N][2], cnt[N][2]; +// 将最短路扩展为二维,含义:最短路与次短路 +// dis:路径长度,cnt:路线数量,st:是否已经出队列 +int dis[N][2], cnt[N][2]; bool st[N][2]; -//链式前向星 +// 链式前向星 int e[M], h[N], idx, w[M], ne[M]; void add(int a, int b, int c = 0) { e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++; @@ -238,64 +240,68 @@ struct Node { // d:目前结点v的路径长度 // k:是最短路0还是次短路1 int u, d, k; - // POJ中结构体,没有构造函数,直接报编译错误 - Node(int u, int d, int k) { - this->u = u, this->d = d, this->k = k; - } const bool operator<(Node x) const { return d > x.d; } }; void dijkrsta() { - priority_queue q; //通过定义结构体小于号,实现小顶堆 - memset(dist, 0x3f, sizeof(dist)); //清空最小距离与次小距离数组 - memset(cnt, 0, sizeof(cnt)); //清空最小距离路线个数与次小距离路线个数数组 - memset(st, 0, sizeof(st)); //清空是否出队过数组 + priority_queue 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 + cnt[s][0] = 1; // 起点s,0:最短路,1:有一条 + cnt[s][1] = 0; // 次短路,路线数为0 - dist[s][0] = 0; //最短路从s出发到s的距离是0 - dist[s][1] = 0; //次短路从s出发到s的距离是0 + dis[s][0] = 0; // 最短路从s出发到s的距离是0 + dis[s][1] = 0; // 次短路从s出发到s的距离是0 - q.push(Node(s, 0, 0)); //入队列 + q.push({s, 0, 0}); // 入队列 while (q.size()) { Node x = q.top(); q.pop(); - int u = x.u, k = x.k, d = x.d; + int u = x.u, k = x.k; // u:节点号,k:是最短路还是次短路,d:路径长度(这个主要用于堆中排序,不用于实战,实战中可以使用dis[u][k]) - if (st[u][k]) continue; //① + if (st[u][k]) continue; // ① 和dijkstra标准版本一样的,只不过多了一个维度 st[u][k] = true; for (int i = h[u]; ~i; i = ne[i]) { int j = e[i]; - int dj = d + w[i]; //原长度+到节点j的边长 + int dj = dis[u][k] + w[i]; // 原长度+到节点j的边长 - if (dj == dist[j][0]) //与到j的最短长度相等,则更新路径数量 + if (dj == dis[j][0]) // 与到j的最短长度相等,则更新路径数量 cnt[j][0] += cnt[u][k]; - else if (dj < dist[j][0]) { //找到更小的路线,需要更新 - dist[j][1] = dist[j][0]; //次短距离被最短距离覆盖 - cnt[j][1] = cnt[j][0]; //次短个数被最短个数覆盖 - - dist[j][0] = dj; //更新最短距离 - cnt[j][0] = cnt[u][k]; //更新最短个数 - - q.push(Node(j, dist[j][1], 1)); //② - q.push(Node(j, dist[j][0], 0)); - } else if (dj == dist[j][1]) //如果等于次短 - cnt[j][1] += cnt[u][k]; //更新次短的方案数,累加 - else if (dj < dist[j][1]) { //如果大于最短,小于次短,两者中间 - dist[j][1] = dj; //更新次短距离 - cnt[j][1] = cnt[u][k]; //更新次短方案数 - q.push(Node(j, dist[j][1], 1)); //次短入队列 + 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}); // 次短入队列 } } } } int main() { +#ifndef ONLINE_JUDGE + freopen("POJ3463.in", "r", stdin); + /* + 答案: + 3 + 2 + */ +#endif int T; scanf("%d", &T); while (T--) { @@ -306,13 +312,14 @@ int main() { scanf("%d %d %d", &a, &b, &c); add(a, b, c); } - //起点和终点 + // 起点和终点 scanf("%d %d", &s, &f); - //计算最短路 + // 计算最短路 dijkrsta(); - //输出 - printf("%d\n", cnt[f][0] + (dist[f][1] == dist[f][0] + 1 ? cnt[f][1] : 0)); + // 输出 + printf("%d\n", cnt[f][0] + (dis[f][1] == dis[f][0] + 1 ? cnt[f][1] : 0)); } return 0; } + ``` diff --git a/TangDou/AcWing_TiGao/T3/MiniPathExtend/POJ3463.cpp b/TangDou/AcWing_TiGao/T3/MiniPathExtend/POJ3463.cpp new file mode 100644 index 0000000..172785f --- /dev/null +++ b/TangDou/AcWing_TiGao/T3/MiniPathExtend/POJ3463.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +using namespace std; +#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]; + +// 链式前向星 +int e[M], h[N], idx, w[M], ne[M]; +void add(int a, int b, int c = 0) { + e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++; +} + +struct Node { + // u: 节点号 + // d:目前结点v的路径长度 + // k:是最短路0还是次短路1 + int u, d, k; + const bool operator<(Node x) const { + return d > x.d; + } +}; + +void dijkrsta() { + priority_queue 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 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}); // 次短入队列 + } + } + } +} +int main() { +#ifndef ONLINE_JUDGE + freopen("POJ3463.in", "r", stdin); + /* + 答案: + 3 + 2 + */ +#endif + int T; + scanf("%d", &T); + while (T--) { + memset(h, -1, sizeof h); + scanf("%d %d", &n, &m); + while (m--) { + int a, b, c; + scanf("%d %d %d", &a, &b, &c); + add(a, b, c); + } + // 起点和终点 + 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; +} diff --git a/TangDou/AcWing_TiGao/T3/MiniPathExtend/POJ3463.in b/TangDou/AcWing_TiGao/T3/MiniPathExtend/POJ3463.in new file mode 100644 index 0000000..e0fe637 --- /dev/null +++ b/TangDou/AcWing_TiGao/T3/MiniPathExtend/POJ3463.in @@ -0,0 +1,19 @@ +2 +5 8 +1 2 3 +1 3 2 +1 4 5 +2 3 1 +2 5 3 +3 4 2 +3 5 4 +4 5 3 +1 5 +5 6 +2 3 1 +3 2 1 +3 1 10 +4 5 2 +5 2 7 +5 2 7 +4 1 \ No newline at end of file