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 <queue>
- using namespace std;
- unordered_map<int, unordered_map<int, int>> graph;
- int main() {
- int n, m, x, y, w, k;
- queue<int> path;
- cin >> n >> m;
- for(int i= 0; i < m; i++){
- cin >> x >> y >> w;
- graph[x][y] = w;
- graph[y][x] = w;
- }
- cin >> k;
- for(int i = 0; i < k; i ++){
- cin >> x;
- path.push(x);
- }
- int result = 0;
- int curr = path.front();
- path.pop();
- while(!path.empty()){
- if(graph[curr][path.front()] == 0){
- result = -1;
- break;
- }
- result += graph[curr][path.front()];
- curr = path.front();
- path.pop();
- }
- cout << result;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment