观光 最短路次短路
// 观光.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
/*
https://www.acwing.com/problem/content/description/385/
“您的个人假期”旅行社组织了一次比荷卢经济联盟的巴士之旅。
比荷卢经济联盟有很多公交线路。
每天公共汽车都会从一座城市开往另一座城市。
沿途汽车可能会在一些城市(零或更多)停靠。
旅行社计划旅途从 S 城市出发,到 F 城市结束。
由于不同旅客的景点偏好不同,所以为了迎合更多旅客,旅行社将为客户提供多种不同线路。
游客可以选择的行进路线有所限制,要么满足所选路线总路程为 S 到 F 的最小路程,
要么满足所选路线总路程仅比最小路程多一个单位长度。
3463_1.png
如上图所示,如果 S=1,F=5,则这里有两条最短路线 1→2→5,1→3→5,长度为 6;有一条比最短路程多一个单位长度的路线 1→3→4→5,长度为 7。
现在给定比荷卢经济联盟的公交路线图以及两个城市 S 和 F,请你求出旅行社最多可以为旅客提供多少种不同的满足限制条件的线路。
输入格式
第一行包含整数 T,表示共有 T 组测试数据。
每组数据第一行包含两个整数 N 和 M,分别表示总城市数量和道路数量。
接下来 M 行,每行包含三个整数 A,B,L,表示有一条线路从城市 A 通往城市 B,长度为 L。
需注意,线路是 单向的,存在从 A 到 B 的线路不代表一定存在从 B 到 A 的线路,另外从城市 A 到城市 B 可能存在多个不同的线路。
接下来一行,包含两个整数 S 和 F,数据保证 S 和 F 不同,并且 S、F 之间至少存在一条线路。
输出格式
每组数据输出一个结果,每个结果占一行。
数据保证结果不超过 109。
数据范围
2≤N≤1000,
1≤M≤10000,
1≤L≤1000,
1≤A,B,S,F≤N
输入样例:
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
输出样例:
3
2
*/
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1010, M = 10010;
int n, m, S, T;
struct ELE {
int id, type, dist;
bool operator > (const ELE& O) const{
return dist > O.dist;
}
};
int h[N], e[M], ne[M], w[M], idx;
int vis[N][2];
int dist[N][2];
int cnt[N][2];
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
int solve() {
memset(dist, 0x3f, sizeof dist);
memset(vis, 0, sizeof vis);
memset(cnt, 0, sizeof cnt);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b, c; cin >> a >> b >> c;
add(a, b, c);
}
cin >> S >> T;
priority_queue<ELE, vector<ELE>, greater<ELE>> q;
q.push({ S,0,0 });
dist[S][0] = 0; cnt[S][0] = 1;
while (!q.empty()) {
auto t = q.top(); q.pop();
int distance = t.dist; int ver = t.id;
int type = t.type; int count = cnt[ver][type];
if (vis[ver][type] != 0) continue;
vis[ver][type] = 1;
for (int i = h[ver]; i != -1; i = ne[i]) {
int j = e[i];
if (dist[j][0] > distance + w[i]) {
dist[j][1] = dist[j][0];
cnt[j][1] = cnt[j][0];
q.push({ j,1,dist[j][1] });
dist[j][0] = distance + w[i];
cnt[j][0] = count;
q.push({ j,0,dist[j][0] });
}
else if (dist[j][0] == (distance + w[i])) {
cnt[j][0] += count;
q.push({ j,0,dist[j][0] });
}
else if (dist[j][1] > (distance + w[i])) {
dist[j][1] = (distance + w[i]);
cnt[j][1] = count;
q.push({ j,1,dist[j][1] });
}
else if (dist[j][1] == (distance + w[i])) {
cnt[j][1] += count;
q.push({ j,1,dist[j][1] });
}
}
}
int res = cnt[T][0];
if (dist[T][0] + 1 == dist[T][1]) res += cnt[T][1];
cout << res << endl;
return res;
}
int main()
{
int t; cin >> t;
while (t--) {
memset(h, -1, sizeof h);
idx = 0;
solve();
}
return 0;
}
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
2021-08-01 STL应用 stack 计蒜客 T1655
2021-08-01 STL应用 map HDU 1263
2021-08-01 STL应用 map poj 2418
2021-08-01 STL应用 set hdu 1412
2019-08-01 Best Cow Line <挑战程序设计竞赛> 习题 poj 3617