Cod sursa(job #2976745)

Utilizator Elvis_CostinTuca Elvis-Costin Elvis_Costin Data 9 februarie 2023 22:22:10
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string np = "distante";
ifstream f(np + ".in");
ofstream g(np + ".out");

// #define f cin
// #define g cout

const ll INF = 1e16L;
vector<ll> d;
vector<pair<int, int>> adj[50003];

bool cmp(vector<ll> a, vector<ll> b)
{
    for (int i = 1; i < a.size(); i++)
        if (a[i] != b[i])
            return 0;
    return 1;
}
void djikstra(int nod)
{
    priority_queue<pair<ll, int>> q;
    q.push({0LL, nod});
    d[nod] = 0;
    while (!q.empty())
    {
        int curr = q.top().second;
        if (-q.top().first > d[curr])
        {
            q.pop();
            continue;
        }
        q.pop();
        for (auto next : adj[curr])
            if (d[next.first] > d[curr] + next.second)
                d[next.first] = d[curr] + next.second,
                q.push({-d[next.first], next.first});
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    f.tie(nullptr);

    int t;
    f >> t;
    for (int z = 1; z <= t; z++)
    {
        int n, m, s;
        f >> n >> m >> s;

        vector<ll> bronzarel(n + 1);
        for (int i = 1; i <= n; i++)
            f >> bronzarel[i];

        for (int i = 1; i <= n; i++)
            adj[i].clear();

        for (int a, b, val, i = 1; i <= m; i++)
            f >> a >> b >> val,
                adj[a].push_back({b, val}),
                adj[b].push_back({a, val});

        d.assign(n + 1, INF);
        djikstra(s);

        if (cmp(d, bronzarel))
            g << "DA\n";
        else
            g << "NU\n";
    }
    return 0;
}