Cod sursa(job #3225067)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 16 aprilie 2024 21:03:58
Problema Sate Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("sate.in");
ofstream fout("sate.out");
const int inf = 1e7 + 2;
struct Muchie {
    int x, y, c;
};
vector<Muchie> a[30002];
int n, m, p, q, i, x, y, c;
bool fr[30002];
int d[30002];

static inline void Dijkastra(int nod) {
    for(i = 1; i <= n; i++) d[i] = inf;
    d[nod] = 0;

    for(int i = 1; i <= n; i++) {
        int mi = inf, poz = 0;
        for(int j = 1; j <= n; j++) {
            if(!fr[j] && d[j] < mi) {
                mi = d[j];
                poz = j;
            }
        }

        if(mi == inf) break;
        fr[poz] = true;

        for(auto vec : a[poz]) {
            d[vec.y] = min(d[vec.y], d[vec.x] + vec.c);
        }
    }
}

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

    Dijkastra(p);

    fout << d[q];

    return 0;
}