main.cc 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <vector>
  4. #include <queue>
  5. using std::vector; using std::queue;
  6. const int N = 100000;
  7. const int M = 200000;
  8. const int C = 1000000000;
  9. struct node {
  10. int x;
  11. int y;
  12. node(int x, int y) : x(x), y(y) {}
  13. };
  14. vector<node> g[N + 1];
  15. int dis[N + 1];
  16. int main() {
  17. int n, m;
  18. scanf("%d %d", &n, &m);
  19. for (int i = 0, p, q, c; i < m; i++) {
  20. scanf("%d %d %d", &p, &q, &c);
  21. if (p == q) continue;
  22. g[p].push_back(node(q, c));
  23. g[q].push_back(node(p, c));
  24. }
  25. memset(dis, -1, sizeof(dis));
  26. queue<int> q;
  27. q.push(n);
  28. dis[n] = 0;
  29. int d = 0, size = q.size();
  30. while (size != 0) {
  31. int cur = q.front();
  32. q.pop();
  33. if (cur == 1) break;
  34. size--;
  35. for (node i : g[cur]) {
  36. if (dis[i.x] == -1) {
  37. dis[i.x] = d + 1;
  38. q.push(i.x);
  39. }
  40. }
  41. if (size == 0) {
  42. size = q.size();
  43. d++;
  44. }
  45. }
  46. printf("%d\n", d);
  47. q = queue<int>();
  48. q.push(1);
  49. while (!q.empty()) {
  50. int cur = q.front();
  51. q.pop();
  52. if (cur == n) break;
  53. }
  54. return 0;
  55. }