• 选择最佳路线(单源最短路扩展应用)


    有一天,琪琪想乘坐公交车去拜访她的一位朋友。

    由于琪琪非常容易晕车,所以她想尽快到达朋友家。

    现在给定你一张城市交通路线图,上面包含城市的公交站台以及公交线路的具体分布。

    已知城市中共包含 n 个车站(编号1~n)以及 m 条公交线路。

    每条公交线路都是 单向的,从一个车站出发直接到达另一个车站,两个车站之间可能存在多条公交线路。

    琪琪的朋友住在 s 号车站附近。

    琪琪可以在任何车站选择换乘其它公共汽车。

    请找出琪琪到达她的朋友家(附近的公交车站)需要花费的最少时间。

    输入格式
    输入包含多组测试数据。

    每组测试数据第一行包含三个整数 n,m,s,分别表示车站数量,公交线路数量以及朋友家附近车站的编号。

    接下来 m 行,每行包含三个整数 p,q,t,表示存在一条线路从车站 p 到达车站 q,用时为 t。

    接下来一行,包含一个整数 w,表示琪琪家附近共有 w 个车站,她可以在这 w 个车站中选择一个车站作为始发站。

    再一行,包含 w 个整数,表示琪琪家附近的 w 个车站的编号。

    输出格式
    每个测试数据输出一个整数作为结果,表示所需花费的最少时间。

    如果无法达到朋友家的车站,则输出 -1。

    每个结果占一行。

    数据范围
    n≤1000,m≤20000,
    1≤s≤n,
    0<w<n,
    0<t≤1000
    输入样例:

    1. 5 8 5
    2. 1 2 2
    3. 1 5 3
    4. 1 3 4
    5. 2 4 7
    6. 2 5 6
    7. 2 3 5
    8. 3 5 1
    9. 4 5 1
    10. 2
    11. 2 3
    12. 4 3 4
    13. 1 2 3
    14. 1 3 4
    15. 2 3 2
    16. 1
    17. 1


    输出样例:

    1. 1
    2. -1

    题目意思是有多个起点可以选择,求到终点的最短距离,暴力每个起点到终点的最短距离再取最小值显然会超时。

    但是我们反过来从终点求到各个起点的最短距离这只需要跑一次最短路,这是一种可行的方法。代码如下:

    1. #include<bits/stdc++.h>
    2. using namespace std; using ll = long long;
    3. int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
    4. //using lll = __int128; template <class T> istream& read(T& x, istream& cin = std::cin) { T num = 0; bool f = 0; char ch = 0; while (!isdigit(ch)) { f |= ch == '-'; if (!cin.get(ch)) return cin; }while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch ^ 48); if (!cin.get(ch)) break; }x = f ? -num : num; return cin; }template <class T> ostream& write(T x, ostream& cout = std::cout) { if (x < 0) cout.put('-'), x = -x; if (x > 9) write(x / 10); cout.put(x % 10 + '0'); return cout; }ostream& operator<<(ostream& cout, lll x) { write(x); return cout; }istream& operator>>(istream& cin, lll& x) { return read(x); }bool check(int i, int j);
    5. bool check(int i, int j);
    6. const int N = 1e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;
    7. int n, m, S, mw, h[N], w[N], e[N], ne[N], idx, dist[N];
    8. vector<int> tar;
    9. inline void add(int a, int b, int c) {
    10. e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
    11. }
    12. void init() {
    13. memset(h, -1, sizeof h);
    14. tar.clear();
    15. cin >> m >> S;
    16. while (m--) {
    17. int a, b, c;
    18. cin >> a >> b >> c;
    19. add(b, a, c);
    20. }
    21. cin >> mw;
    22. while (mw--) {
    23. int x;
    24. cin >> x;
    25. tar.emplace_back(x);
    26. }
    27. //cout << "init successful" << endl;
    28. return;
    29. }
    30. bool st[N];
    31. int spfa() {
    32. queue<int> qu;
    33. qu.emplace(S);
    34. memset(dist, 0x3f, sizeof dist);
    35. dist[S] = 0;
    36. while (qu.size()) {
    37. int x = qu.front();
    38. qu.pop();
    39. st[x] = false;
    40. for (int i = h[x]; ~i; i = ne[i]) {
    41. int nx = e[i], td = w[i];
    42. if (dist[nx] > dist[x] + td) {
    43. dist[nx] = dist[x] + td;
    44. if (st[nx]) continue;
    45. st[nx] = true;
    46. qu.emplace(nx);
    47. }
    48. }
    49. }
    50. int res = INF;
    51. for (auto& ed : tar) {
    52. res = min(res, dist[ed]);
    53. }
    54. return res > INF / 2 ? -1 : res;
    55. }
    56. void solve() {
    57. cout << spfa();
    58. //cout << "all successful" << endl;
    59. return;
    60. }
    61. int main(void) {
    62. ios::sync_with_stdio(0); cin.tie(0); cout << setprecision(6) << fixed;
    63. tar.reserve(N);
    64. int TT = 1;
    65. //cin >> TT;
    66. for (int ii = 1; cin >> n; init(), solve(), ii++, cout << "\n") {}
    67. return 0;
    68. }

    其次我们可以创建一个到各个起点距离都是 0 的点,再以该点作为起点跑最短路也是一种可行的方案。代码如下:

    1. #include<bits/stdc++.h>
    2. using namespace std; using ll = long long;
    3. int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
    4. //using lll = __int128; template <class T> istream& read(T& x, istream& cin = std::cin) { T num = 0; bool f = 0; char ch = 0; while (!isdigit(ch)) { f |= ch == '-'; if (!cin.get(ch)) return cin; }while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch ^ 48); if (!cin.get(ch)) break; }x = f ? -num : num; return cin; }template <class T> ostream& write(T x, ostream& cout = std::cout) { if (x < 0) cout.put('-'), x = -x; if (x > 9) write(x / 10); cout.put(x % 10 + '0'); return cout; }ostream& operator<<(ostream& cout, lll x) { write(x); return cout; }istream& operator>>(istream& cin, lll& x) { return read(x); }bool check(int i, int j);
    5. bool check(int i, int j);
    6. const int N = 1e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;
    7. int n, m, S, mw, h[N], w[N], e[N], ne[N], idx, dist[N];
    8. inline void add(int a, int b, int c) {
    9. e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
    10. }
    11. void init() {
    12. memset(h, -1, sizeof h);
    13. cin >> m >> S;
    14. while (m--) {
    15. int a, b, c;
    16. cin >> a >> b >> c;
    17. add(a, b, c);
    18. }
    19. cin >> mw;
    20. while (mw--) {
    21. int x;
    22. cin >> x;
    23. add(0, x, 0);
    24. }
    25. //cout << "init successful" << endl;
    26. return;
    27. }
    28. bool st[N];
    29. int spfa() {
    30. queue<int> qu;
    31. qu.emplace(0);
    32. memset(dist, 0x3f, sizeof dist);
    33. dist[0] = 0;
    34. while (qu.size()) {
    35. int x = qu.front();
    36. qu.pop();
    37. st[x] = false;
    38. for (int i = h[x]; ~i; i = ne[i]) {
    39. int nx = e[i], td = w[i];
    40. if (dist[nx] > dist[x] + td) {
    41. dist[nx] = dist[x] + td;
    42. if (st[nx]) continue;
    43. st[nx] = true;
    44. qu.emplace(nx);
    45. }
    46. }
    47. }
    48. return dist[S] > INF / 2 ? -1 : dist[S];
    49. }
    50. void solve() {
    51. cout << spfa();
    52. //cout << "all successful" << endl;
    53. return;
    54. }
    55. int main(void) {
    56. ios::sync_with_stdio(0); cin.tie(0); cout << setprecision(6) << fixed;
    57. int TT = 1;
    58. //cin >> TT;
    59. for (int ii = 1; cin >> n; init(), solve(), ii++, cout << "\n") {}
    60. return 0;
    61. }

  • 相关阅读:
    Docker逃逸---CVE-2020-15257浅析
    怎么把相册的某一张压缩作为相册的缩略图?
    【全民Python】PIP模块的安装,Pyinstaller模块安装,导出exe文件
    GB28181安防视频融合汇聚平台EasyCVR如何实现视频画面自定义标签?
    100. Go单测系列0---单元测试基础
    Python | eval、exec | 执行动态代码字符串
    Java网络编程
    网络——IPV4地址(二)
    Java中的读写锁ReentrantReadWriteLock详解,存在一个小缺陷
    Jtti:linux中jboss无法启动怎么解决
  • 原文地址:https://blog.csdn.net/weixin_53469462/article/details/125555887