Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include <unordered_map>
- #include <set>
- #include <queue>
- using namespace std;
- unordered_map<int, unordered_map<int, int>> graph;
- int isPath(queue<int>& path) {
- int current = path.front();
- path.pop();
- int sum = 0;
- while(!path.empty()){
- int top = path.front();
- path.pop();
- if(graph[current].find(top) == graph[current].end()){
- return -1;
- }
- sum += graph[current][top];
- current = top;
- }
- return sum;
- }
- int main() {
- int n, m, x, y, w, k;
- cin >> n >> m;
- for(int i =0; i < m; i++){
- cin >> x >> y >> w;
- graph[x][y] = w;
- graph[y][x] = w;
- }
- cin >> k;
- queue<int> path;
- for(int i = 0; i < k; i++){
- cin >> x;
- path.push(x);
- }
- cout << isPath(path);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment