Cod sursa(job #2121705)

Utilizator UWantMyNameGheorghe Vlad Camil UWantMyName Data 4 februarie 2018 11:02:46
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <bits/stdc++.h>
#define in "sate.in"
#define out "sate.out"
#define Nmax 30003
using namespace std;
ifstream fin(in);
ofstream fout(out);

struct sate
{
    int nod;
    int dist;
};

int n,m;
int X,Y;
int d[Nmax];
vector <sate> L[Nmax];
queue <int> q;

void Read()
{
    int x,y,cost,i;
    sate s;

    fin >> n >> m >> X >> Y;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y >> cost;

        s.nod = y;
        s.dist = cost;
        L[x].push_back(s);

        s.nod = x;
        L[y].push_back(s);
    }
}

void Init()
{
    int i;

    for (i = 1; i <= n; i++)
        d[i] = -1;
}

void BFS(int k)
{
    int nod,cost;

    q.push(k);
    d[k] = 0;
    while(!q.empty())
    {
        k = q.front();
        q.pop();
        for (auto i : L[k])
        {
            nod = i.nod;
            cost = i.dist;
            if (d[nod] == -1)
            {
                if (nod > k) d[nod] = d[k] + cost;
                else d[nod] = d[k] - cost;
                q.push(nod);
            }
        }
    }
}

int main()
{
    Read();
    Init();
    BFS(X);
    fout << d[Y] << "\n";

    fin.close();
    fout.close();
    return 0;
}