Cod sursa(job #3272331)

Utilizator zavragiudavid dragoi zavragiu Data 29 ianuarie 2025 09:34:14
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <bits/stdc++.h>
using namespace std;

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

struct Trei
{
    int x, y, cost;
};

struct Trey
{
    ///  nod    cost    nod din care vin
    int first, second, third;
};

int n, m, X, Y;
vector<pair <int, int>> G[30002];
queue<int> q;
bool ajuns;
int d[30002];
bitset<30002> viz;

void BFS()
{
    int nod, cost, vin, a, b;
    q.push(X);
    d[X] = 0;
    while(!q.empty())
    {
        int t = q.front();
        q.pop();
        for(auto w : G[t])
            if(!viz[w.first])
            {
                viz[w.first] = true;
                if(w.first > t) d[w.first] = d[t] + w.second;
                else d[w.first] = d[t] - w.second;
                q.push(w.first);
            }
    }
}

int main()
{
    int i, j, c;
    fin >> n >> m >> X >> Y;
    while(m--)
    {
        fin >> i >> j >> c;
        G[i].push_back({j, c});
        G[j].push_back({i, c});
    }
    BFS();
    fout << d[Y];
    return 0;
}