Cod sursa(job #3202011)

Utilizator AlbuDariusAlbu Darius AlbuDarius Data 10 februarie 2024 12:48:48
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <fstream>

using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int a[3][200050], start[30005], c[30005], d[30005], viz[30005], y1;
void bfs(int nod);
int main()
{
    int n, m, x1;
    fin >> n >> m >> x1 >> y1;
    int x, y, k = 0, i;
    for (i = 1; i <= m; i++) {
        k++;
        fin >> x >> y >> a[2][k];
        a[0][k] = y;
        a[1][k] = start[x];
        start[x] = k;
        k++;
        a[0][k] = x;
        a[1][k] = start[y];
        start[y] = k;
        a[2][k] = a[2][k - 1];
    }
    bfs(x1);
    fout << d[y1];
    return 0;
}
void bfs(int nod)
{
    int ps = 1, pi = 1, x;
    c[ps] = nod;
    viz[nod] = 1;
    while (pi <= ps && viz[y1] == 0) {
        x = start[c[pi]];
        while (x) {
            if (viz[a[0][x]] == 0) {
                viz[a[0][x]] = 1;
                if (c[ps] > a[0][x])
                    d[a[0][x]] = d[c[ps]] - a[2][x];
                else
                    d[a[0][x]] = d[c[ps]] + a[2][x];
                c[++ps] = a[0][x];
            }
            x = a[1][x];
        }
        pi++;
    }
}