Cod sursa(job #3188598)

Utilizator gabriel.9619Gabriel Stefan Tita gabriel.9619 Data 3 ianuarie 2024 14:37:19
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <fstream>
#include <vector>
#include <queue>
#define dim 30100

using namespace std;

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

int n, m, x, y;
int dist[dim];
queue<int> q;
vector<pair<int, int>> l[dim];

void bfs(int start) {
    q.push(start);
    dist[start] = 0;
    while (!q.empty()) {
        int node = q.front();
        q.pop();

        for (auto adjElem : l[node]) {
            int adjNode = adjElem.first;
            int adjDist = adjElem.second;
            if (dist[adjNode] == -1) {
                if (node < adjNode)
                    dist[adjNode] = dist[node] + adjDist;
                else
                    dist[adjNode] = dist[node] - adjDist;
                q.push(adjNode);
            }
        }
    }
}

int main() {
    fin >> n >> m >> x >> y;
    for (int i = 1; i <= m; i++) {
        int a, b, c;
        fin >> a >> b >> c;
        l[a].push_back(make_pair(b, c));
        l[b].push_back(make_pair(a, c));
    }

    for (int i = 1; i <= n; i++)
        dist[i] = -1;
    bfs(x);

    fout << dist[y];

    return 0;
}