Cod sursa(job #3148381)

Utilizator andrei1807Andrei andrei1807 Data 31 august 2023 16:52:49
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

const int NMAX = 30005;

vector<pair<int, int> > g[NMAX];
int d[NMAX];

void bfs(int nod) {
    bool vis[NMAX] = {false};
    queue<int> q;
    q.push(nod);
    vis[nod] = true;

    while (!q.empty()) {
        int curr = q.front();
        q.pop();

        for (auto next : g[curr]) {
            int next_nod = next.first, c = next.second;
            if (!vis[next_nod] || (d[curr] + c < d[next_nod])) {
                vis[next_nod] = true;
                d[next_nod] = d[curr] + c;
                q.push(next_nod);
            }
        }
    }
}

int main() {

    int n, m, p, q;
    fin >> n >> m >> p >> q;
    while (m--) {
        int x, y, c;
        fin >> x >> y >> c;

        g[x].push_back(make_pair(y, c));
        g[y].push_back(make_pair(x, -c));
    }
    bfs(p);

    fout << d[q];
    
    return 0;
}