Cod sursa(job #2785877)

Utilizator Albert_GAlbert G Albert_G Data 19 octombrie 2021 19:17:34
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <fstream>
#include <vector>
#include <queue>

std::ifstream in("sate.in");
std::ofstream out("sate.out");

const int N = 3e4+1;
std::vector<std::pair<int,int>> g[N];
bool vis[N];
int res, x, y;

void bfs(int i){
    std::queue<std::pair<int,int>> q;
    q.push({i,0});
    vis[i] = 1;
    bool found = false;
    while(!q.empty() && !found){
        int currNode = q.front().first, currDist = q.front().second;
        q.pop();
        for(auto node : g[currNode]){
            if(vis[node.first]) continue;
            int dist = (node.first > currNode ? node.second : -node.second);
            vis[node.first] = 1;
            if(node.first == y){
                res = currDist + dist;
                found = true;
                return;
            }
            q.push({node.first, currDist + dist});
        }
    }
}   

int main(){
    int n,m;
    in>>n>>m>>x>>y;
    for(int i=0;i<m;++i){
        int a, b, dist;
        in>>a>>b>>dist;
        g[a].push_back({b,dist});
        g[b].push_back({a,dist});
    }
    bfs(x);
    out<<res;
    in.close();
    out.close();
    return 0;
}