Cod sursa(job #3275706)

Utilizator camelia22Dragoiu Camelia camelia22 Data 11 februarie 2025 16:45:21
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <iostream>
#include <fstream>
#define N 30005
#define M 100050
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
int n, m, st, dr, i, x, y, t[3][2*M], start[N], viz[N], k, z, cost[N], c[N];

void bfs_liste(int st)
{
    int pi, ps, k, man;
    pi = ps = 1;
    c[pi] = st; viz[st] = 1;
    while (ps <= pi)
    {
        k = c[ps];
        man = start[k];
        while (man)
        {
            if (viz[t[0][man]] == 0)
            {
                c[++pi] = t[0][man];
                viz[t[0][man]] = 1;
                if (k < t[0][man])
                    cost[t[0][man]] = cost[k] + t[2][man];
                else
                    cost[t[0][man]] = cost[k] - t[2][man];
            }
            man = t[1][man];
        }
        ps++;
    }
}
int main()
{
    f >> n >> m >> st >> dr;
    for (i = 1; i <= m; i++)
    {
        f >> x >> y >> z;
        k++;
        t[0][k] = y;
        t[1][k] = start[x];
        start[x] = k;
        t[2][k] = z;
        k++;
        t[0][k] = x;
        t[1][k] = start[y];
        start[y] = k;
        t[2][k] = z;
    }
    bfs_liste(st);
    g << cost[dr];
    return 0;
}