Cod sursa(job #2884833)

Utilizator AswVwsACamburu Luca AswVwsA Data 4 aprilie 2022 23:42:28
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
//uh, bfs simplu??
#include <fstream>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
const int NMAX = 7504;
vector <pair <int, int> > g[NMAX];
bool viz[NMAX];
int d[NMAX];
queue <int> q;
int main()
{
    ifstream cin("sate.in");
    ofstream cout("sate.out");
    int n, m, x, y, i;
    cin >> n >> m >> x >> y;
    for (i = 1; i <= m; i++)
    {
        int a, b, d;
        cin >> a >> b >> d;
        g[a].push_back({b, d});
        g[b].push_back({a, d});
    }
    q.push(x);
    viz[x] = 1;
    while (!q.empty())
    {
        int f = q.front();
        q.pop();
        for (pair <int, int> v : g[f])
            if (!viz[v.first])
        {
            viz[v.first] = 1;
            d[v.first] = d[f] + v.second;
            if (v.first == y)
                break;
            q.push(v.first);
        }
    }
    cout << d[y];
}