Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #include <queue>
- #include <vector>
- using namespace std;
- const int maxn = 1e5 + 5;
- typedef long long ll;
- int n, m;
- vector<pair<int, int>> graph[maxn];
- vector<bool> visited(maxn, false);
- ll bfs(int S, int E){
- queue<pair<int, ll>> q;
- q.push({S, 0});
- visited[S] = true;
- while(!q.empty()){
- int node = q.front().first;
- ll total = q.front().second;
- q.pop();
- if(node == E){
- return llabs(total);
- }
- for(pair<int,int> edge : graph[node]){
- int neighbour = edge.first;
- int w = edge.second;
- if(!visited[neighbour]){
- visited[neighbour] = true;
- ll nov_total = total;
- if(neighbour > node){ //poradi tezinata, odam so ovoj nacin, ne so nova varijabla koja ja stavam vo queue
- nov_total += w;
- }
- else{
- nov_total -= w;
- }
- q.push({neighbour, nov_total});
- }
- }
- }
- return -1;
- }
- int main() {
- ios::sync_with_stdio(false);
- int t1, t2;
- cin >> t1 >> t2;
- cin >> n >> m;
- for(int i = 0; i < m; i++){
- int a, b, c;
- cin >> a >> b >> c;
- graph[a].push_back({b, c});
- graph[b].push_back({a, c});
- }
- cout << bfs(t1, t2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment