Advertisement
kokokozhina

103

May 10th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <queue>
  6.  
  7. using namespace std;
  8.  
  9. int n, m;
  10. vector<vector<pair<int, int>>> g;
  11. const int N = 201;
  12. const int INF = 1000000000;
  13. int d[N][N][4];
  14. int p[N];
  15.  
  16. void bfs(){
  17. for(int k = 0; k < N; k++){
  18. for(int i = 0; i < N; i++)
  19. for(int j = 1; j < 4; j++){
  20. d[k][i][j] = INF;
  21. }
  22. p[k] = -1;
  23. }
  24. p[0] = 0;
  25. d[0][0][1] = d[0][0][2] = d[0][0][3] = 0;
  26. vector<bool> used(n);
  27. used[0] = true;
  28. queue<int> q;
  29. q.push(0);
  30. while(!q.empty()){
  31. int u = q.front();
  32. if(u == n-1){
  33. cout << min(min(d[p[u]][u][1], d[p[u]][u][2]), d[p[u]][u][3]) << endl; //?
  34. exit(0);
  35. }
  36. q.pop();
  37. for(int i = 0; i < g[u].size(); i++){
  38. if(!used[i] && g[u][i]){
  39. q.push(i);
  40. used[i] = true;
  41. p[i] = u;
  42. d[p[u]][i][g[u][i].second] = d[u] + 1;
  43. }
  44. }
  45. }
  46. cout << -1 << endl;
  47. }
  48.  
  49. int main()
  50. {
  51. #ifdef _DEBUG
  52. freopen("input.txt", "r", stdin);
  53. freopen("output.txt", "w", stdout);
  54. #endif
  55. scanf("%d%d", &n, &m);
  56. g = vector<vector<pair<int, int>>> (n);
  57. for(int i = 0; i < m; i++)
  58. {
  59. int x, y, c;
  60. scanf("%d%d%d", &x, &y, &c);
  61. x--; y--;
  62. g[x].push_back(make_pair(y, c));
  63. }
  64.  
  65. bfs();
  66.  
  67. return 0;
  68. }//bug
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement