Cod sursa(job #2664672)

Utilizator georgeblanarBlanar George georgeblanar Data 29 octombrie 2020 09:21:51
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

ifstream f("sate.in");
ofstream g("sate.out");

int n, m, x, y;
vector< pair<int, int> > adj[30005];
int dist[30005];
bool viz[30005];

void BFS()
{
    int s, next, distanta;
    queue<int> queue;
    queue.push(x);
    viz[x] = true;
    while (!queue.empty())
    {
        s = queue.front();
        queue.pop();
        for (auto rel : adj[s])
        {
            next = rel.first;
            distanta = rel.second;
            if (!viz[next])
            {
                viz[next] = true;
                dist[next] = dist[s] + distanta;

                queue.push(next);

                if (next == y)
                {
                    g << dist[y] << "\n";
                    return;
                }
            }
        }
    }
}

int main()
{

    f >> n >> m >> x >> y;
    while(m--)
    {
        int a, b, distanta;
        f >> a >> b >> distanta;
        adj[a].push_back({b, distanta});
        adj[b].push_back({a, -distanta});
    }
    BFS();
    return 0;
}