Cod sursa(job #2465376)

Utilizator aurelionutAurel Popa aurelionut Data 30 septembrie 2019 01:08:09
Problema Sate Scor 60
Compilator cpp-32 Status done
Runda Arhiva de probleme Marime 1.09 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];
queue < pair <int, int> > q;

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(make_pair(X, 0));
    int ans = -1;
    while (!q.empty())
    {
        int node = q.front().first;
        int cost = q.front().second;
        if (node == Y)
        {
            ans = cost;
            break;
        }
        q.pop();
        for (auto &next : graph[node])
        {
            if (next.first > node)
                q.push(make_pair(next.first, cost + next.second));
            else
                q.push(make_pair(next.first, cost - next.second));
        }
    }
    fout << ans << "\n";
    fin.close();
    fout.close();
    return 0;
}