D_L3

test 6 - 2022-2023 - Проверка на път

Jan 8th, 2024
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <unordered_map>
  7. #include <queue>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. unordered_map<int, unordered_map<int, int>> graph;
  13.  
  14. int main() {
  15. int n, m, x, y, w, k;
  16. queue<int> path;
  17. cin >> n >> m;
  18. for(int i= 0; i < m; i++){
  19. cin >> x >> y >> w;
  20. graph[x][y] = w;
  21. graph[y][x] = w;
  22. }
  23. cin >> k;
  24. for(int i = 0; i < k; i ++){
  25. cin >> x;
  26. path.push(x);
  27. }
  28.  
  29. int result = 0;
  30. int curr = path.front();
  31. path.pop();
  32. while(!path.empty()){
  33. if(graph[curr][path.front()] == 0){
  34. result = -1;
  35. break;
  36. }
  37. result += graph[curr][path.front()];
  38. curr = path.front();
  39. path.pop();
  40. }
  41. cout << result;
  42. return 0;
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment