fork download
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. #define pll pair<int,int>
  5. #define inf 100000009
  6.  
  7. using namespace std;
  8. int n,m,st,en;
  9. int a,b,w;
  10. vector<int> v[510];
  11. priority_queue<pll,vector<pll>,greater<pll>> pq;
  12. int dist[510];
  13. vector<int> route[510];
  14. int we[510][510];
  15. bool check[510];
  16.  
  17. void di(int x){
  18. pq.push({0,x});
  19. dist[x] = 0;
  20. while(!pq.empty()){
  21. int cost = pq.top().first;
  22. int start = pq.top().second;
  23. pq.pop();
  24.  
  25. cout << "start : " << start << ", cost : " << cost << '\n';
  26.  
  27. for(int i=0; i<v[start].size(); i++){
  28. int cc = we[start][v[start][i]];
  29. if(cc==-1){
  30. continue;
  31. }
  32. if(dist[v[start][i]] >= cost+cc){
  33. if(dist[v[start][i]]>cost+cc){
  34. route[v[start][i]].clear();
  35. }
  36. dist[v[start][i]] = cost+cc;
  37. route[v[start][i]].push_back(start);
  38. pq.push({dist[v[start][i]],v[start][i]});
  39. }
  40. }
  41. }
  42. }
  43.  
  44. void del(int x){
  45. vector<int> r;
  46. r.push_back(x);
  47. while(!r.empty()){
  48. int s = r.back();
  49. r.pop_back();
  50. for(int i=0; i<route[s].size(); i++){
  51. we[route[s][i]][s] = -1;
  52. if(check[route[s][i]]){
  53. continue;
  54. }
  55. check[route[s][i]] = true;
  56. r.push_back(route[s][i]);
  57. }
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. while(1){
  64. cin >> n >> m;
  65. if(n==0 && m==0){
  66. break;
  67. }
  68. cin >> st >> en;
  69. for(int i=0; i<510; i++){
  70. v[i].clear();
  71. route[i].clear();
  72. check[i] = false;
  73. }
  74. for(int i=0; i<510; i++){
  75. for(int j=0; j<510; j++){
  76. we[i][j] = -1;
  77. }
  78. }
  79. for(int i=0; i<m; i++){
  80. cin >> a >> b >> w;
  81. v[a].push_back(b);
  82. we[a][b] = w;
  83. }
  84. for(int i=0; i<510; i++){
  85. dist[i] = inf;
  86. }
  87. di(st);
  88. del(en);
  89. for(int i=0; i<510; i++){
  90. dist[i] = inf;
  91. }
  92. di(st);
  93. if(dist[en]==inf){
  94. cout << -1 << "\n";
  95. }
  96. else{
  97. cout << dist[en] << "\n";
  98. }
  99. }
  100. }
  101.  
  102. /*
  103. 9 12
  104. 0 8
  105. 0 1 1
  106. 0 2 2
  107. 0 3 3
  108. 1 4 3
  109. 2 4 2
  110. 3 4 1
  111. 4 5 1
  112. 4 6 2
  113. 4 7 3
  114. 5 8 3
  115. 6 8 2
  116. 7 8 1
  117. 0 0
  118. */
Success #stdin #stdout 0s 5304KB
stdin
9 12
0 8
0 1 1
0 2 2
0 3 3
1 4 3
2 4 2
3 4 1
4 5 1
4 6 2
4 7 3
5 8 3
6 8 2
7 8 1
0 0
stdout
start : 0, cost : 0
start : 1, cost : 1
start : 2, cost : 2
start : 3, cost : 3
start : 4, cost : 4
start : 4, cost : 4
start : 4, cost : 4
start : 5, cost : 5
start : 5, cost : 5
start : 5, cost : 5
start : 6, cost : 6
start : 6, cost : 6
start : 6, cost : 6
start : 7, cost : 7
start : 7, cost : 7
start : 7, cost : 7
start : 8, cost : 8
start : 8, cost : 8
start : 8, cost : 8
start : 8, cost : 8
start : 8, cost : 8
start : 8, cost : 8
start : 8, cost : 8
start : 8, cost : 8
start : 8, cost : 8
start : 0, cost : 0
-1