Cod sursa(job #3201654)

Utilizator VVasiuVasiu Vasile VVasiu Data 9 februarie 2024 13:11:11
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <iostream>
#include <fstream>
using namespace std;
#define N 30001
ifstream fin("sate.in");
ofstream fout("sate.out");

int n, m, a, b, x, y, val, k = 0;
int t[3][2*100025], start[N], viz[N], c[N], cost[N];


void bfs(int plec)
{
    int st, dr, man, drmin = 0;
    st = dr = 1;
    viz[plec] = 1;
    c[dr] = plec;
    cost[plec] = 0;
    while(st <= dr)
    {
        man = start[c[st]];
        while( man )
        {
            if( !viz[t[0][man]])
            {
                viz[t[0][man]] = 1;
                if(t[0][man] > c[st] && cost[c[st]] + t[2][man] < cost[t[0][man]])
                    cost[t[0][man]] = cost[c[st]] + t[2][man];
                else
                    if(t[0][man] < c[st] && cost[c[st]] - t[2][man] < cost[t[0][man]])
                        cost[t[0][man]] = cost[c[st]] - t[2][man];
                c[++dr] = t[0][man];
            }
            man = t[1][man];
        }
        st++;
    }
    fout << cost[b];
}
int main()
{
    fin >> n >> m >> a >> b;
    while(fin >> x >> y >> val)
    {
        t[0][++k] = y;
        t[1][k] = start[x];
        t[2][k] = val;
        start[x] = k;

        t[0][++k] = x;
        t[1][k] = start[y];
        t[2][k] = val;
        start[y] = k;
    }
    for(int i=1; i<=n; i++)
        cost[i] = 999999999;
    bfs(a);
    return 0;
}