Cod sursa(job #3370060)

Utilizator Razvan23Razvan Mosanu Razvan23 Data 23 octombrie 2026 20:19:34
Problema Sate Scor 35
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("sate.in");
ofstream fout("sate.out");

int n, m, X, Y;
vector<vector<pair<int, int>>> L;
queue<pair<int, int>> q;
vector<int> viz;

void BFS()
{
    q.push({0, X});
    viz[X] = 1;
    while(!q.empty())
    {
        int dist = q.front().first;
        int nod = q.front().second;
        q.pop();
        for(const auto& w : L[nod])
            if(viz[w.first] == 0)
            {
                int cost = w.second;
                int destinatie = w.first;
                viz[w.first] = 1;
                if(destinatie == Y) 
                {
                    fout << dist + cost;
                    exit(0);
                }
                if(nod < destinatie) q.push({dist + cost, destinatie});
                else q.push({dist - cost, destinatie});
            }
    }
}

int main()
{
    int i, x, y, c;
    fin >> n >> m >> X >> Y;
    L.resize(n + 1);
    viz.resize(n + 1, 0);
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y >> c;
        L[x].push_back({y, c});
        L[y].push_back({x, c});
    }
    BFS();
    fin.close();
    fout.close();
    return 0;
}