Cod sursa(job #2465379)

Utilizator aurelionutAurel Popa aurelionut Data 30 septembrie 2019 01:14:30
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <queue>
#include <fstream>
#include <algorithm>

using namespace std;

const int NMAX = 30005;
int nodes, edges, X, Y;
vector < pair <int, int> > graph[NMAX];
int dist[NMAX];
queue <int> q;
bool inq[NMAX];

int main()
{
    ifstream fin("sate.in");
    ofstream fout("sate.out");
    fin >> nodes >> edges >> X >> Y;
    for (int i = 1;i <= edges;++i)
    {
        int x, y, d;
        fin >> x >> y >> d;
        graph[x].push_back(make_pair(y, d));
        graph[y].push_back(make_pair(x, d));
    }
    q.push(X);
    inq[X] = true;
    while (!q.empty())
    {
        int node = q.front();
        inq[node] = false;
        q.pop();
        for (auto &next : graph[node])
        {
            if (dist[next.first] == 0)
            {
                if (next.first > node)
                    dist[next.first] = dist[node] + next.second;
                else
                    dist[next.first] = dist[node] - next.second;
                if (inq[next.first] == false)
                {
                    q.push(next.first);
                    inq[next.first] = true;
                }
            }
        }
    }
    fout << dist[Y] << "\n";
    fin.close();
    fout.close();
    return 0;
}