Cod sursa(job #2834445)

Utilizator Teodor_AxinteAxinte Teodor-Ionut Teodor_Axinte Data 16 ianuarie 2022 23:36:12
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("distante.in");
ofstream fout("distante.out");

const int N = 100010;
const int oo = 99999999;

int n, m, viz[N], d[N], zaharel[N];
vector<pair<int, int>> v[N];
priority_queue<pair<int, int>> q;

void Dijkstra(int source)
{
    int minn, i, k, pas, cost;
    for (int i = 1; i <= n; i++)
        if (i != source)
            d[i] = oo;
        else
            d[i] = 0;
    q.push({0, source});

    while (!q.empty())
    {
        k = q.top().second;
        q.pop();

        if (viz[k] == 0)
        {
            viz[k] = 1;
            for (auto it : v[k])
            {
                cost = it.first;
                i = it.second;
                if (d[i] > d[k] + cost)
                {
                    d[i] = d[k] + cost;
                    q.push({-d[i], i});
                }
            }
        }
    }

    for (int i = 1; i <= n; i++)
        if (d[i] == oo)
            d[i] = 0;
}

string ver(int *a, int *b, int n)
{
    for (int i = 1; i <= n; i++)
        if (a[i] != b[i])
            return "NU";
    return "DA";
}

int main()
{

    int probleme;
    fin >> probleme;
    for (; probleme != 0; probleme--)
    {
        int x, y, cost, plecare;
        fin >> n >> m >> plecare;

        for (int i = 1; i <= n; i++)
            fin >> zaharel[i];
        for (int i = 1; i <= m; i++)
        {
            fin >> x >> y >> cost;
            v[x].push_back({cost, y});
            v[y].push_back({cost, x});
        }

        for (int i = 1; i <= n; i++)
            viz[i] = 0;
        Dijkstra(plecare);

        fout << ver(d, zaharel, n) << '\n';
    }

    /*Dijkstra(1);

    for (int i = 1; i <= n; i++)
        if (d[i] == oo)
            fout << "0 ";
        else
            fout << d[i] << " ";

    */
    return 0;
}