1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 55;
  4. int g[N][N];
  5. int n, m;
  6. bool st[N];
  7. int d[N];
  8. int S;
  9. void dijkstra()
  10. {
  11. memset(d,0x3f,sizeof d);
  12. d[S] = 0;
  13. for(int i = 0; i < n; i++)
  14. {
  15. int t = -1;
  16. for(int j = 0; j < n; j++)
  17. {
  18. if(!st[j] && (t == -1 || d[t] > d[j]))
  19. t = j;
  20. }
  21. st[t] = true;
  22. for(int j = 0; j < n; j++)
  23. {
  24. if(g[t][j] != 0) d[j] = min(d[j],d[t]+g[t][j]);
  25. }
  26. }
  27. }
  28. int main()
  29. {
  30. cin >> n >> S;
  31. for(int i = 0; i < n; i++)
  32. {
  33. for(int j = 0; j < n; j++)
  34. {
  35. cin >> g[i][j];
  36. }
  37. }
  38. dijkstra();
  39. for(int i = 0; i < n; i++)
  40. {
  41. if(i != S)
  42. {
  43. if(d[i] == 0x3f3f3f3f) cout << -1 << ' ';
  44. else cout << d[i] << ' ';
  45. }
  46. }
  47. cout << endl;
  48. return 0;
  49. }
点赞(0)
 

9.9 分

1 人评分

 

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 0 条评论

暂无评论