Cod sursa(job #2583253)

Utilizator BAlexandruBorgovan Alexandru BAlexandru Data 17 martie 2020 22:39:22
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <bits/stdc++.h>

using namespace std;

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

int T;
int n, m, s, i;
int x, y, lg;
int distBronz[50001], distCorect[50001];
vector < pair < int, int > > v[50001];

void BellmanFord(int sursa)
{
    queue < int > coada;
    coada.push(sursa);
    while (!coada.empty())
    {
        int nod = coada.front();
        coada.pop();
        for (int i=0; i<v[nod].size(); i++)
            if (v[nod][i].first != sursa)
                if (distCorect[v[nod][i].first] == 0  ||  distCorect[nod] + v[nod][i].second < distCorect[v[nod][i].first])
                {
                    distCorect[v[nod][i].first] = distCorect[nod] + v[nod][i].second;
                    coada.push(v[nod][i].first);
                }
    }
}

int main()
{
    f >> T;
    while (T--)
    {
        f >> n >> m >> s;

        for (i=1; i<=n; i++)
        {
            f >> distBronz[i];
            v[i].clear();
        }

        for (i=1; i<=m; i++)
        {
            f >> x >> y >> lg;
            v[x].push_back(make_pair(y, lg));
            v[y].push_back(make_pair(x, lg));
        }

        memset(distCorect, 0, sizeof(distCorect));
        BellmanFord(s);

        bool t = true;
        for (i=1; i<=n && t; i++)
            if (distBronz[i] != distCorect[i])
                t = false;

        if (t)
            g << "DA\n";
        else
            g << "NU\n";
    }
    return 0;
}