Cod sursa(job #3327940)

Utilizator Andrei_Gagea08Andrei Gagea Andrei_Gagea08 Data 5 decembrie 2025 17:55:09
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <bits/stdc++.h>
#define int long long

using namespace std;

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

struct vect{
    int t,c;
    bool operator<(vect other)const
    {
        return this->c < other.c;
    };
}aux;
int n,m,S;

vector<int> dijkstra(int nod,vector<vector<vect>> adj)
{
    vector<int> sol(n+2);
    multiset<vect> s;

    for(int i=0;i<=n;i++)
        sol[i] = LLONG_MAX;

    aux.t=nod;
    aux.c=0;
    s.insert(aux);
    while(!s.empty())
    {
        vect ts = *s.begin();
        s.erase(s.begin());
        if(sol[ts.t] > ts.c)
        {
            sol[ts.t] = ts.c;
            for(auto it:adj[ts.t])
            {
                aux.c = ts.c + it.c;
                aux.t = it.t;
                s.insert(aux);
            }
        }
    }

    sol[nod] = 0;
    return sol;
}

void solve(int testcase)
{
    in>>n>>m>>S;
    vector<vector<vect>> v(n+2);
    vector<int> RASP(n+2);

    for(int i=1;i<=n;i++)
        in>>RASP[i];

    for(int i=1;i<=m;i++)
    {
        int a,b;
        in>>a>>b>>aux.c;
        aux.t = b;
        v[a].push_back(aux);
        aux.t = a;
        v[b].push_back(aux);
    }

    vector<int> sol = dijkstra(S,v);

    for(int i=1;i<=n;i++)
        if(RASP[i]!=sol[i])
        {
            out<<"NU\n";
            return;
        }
    out<<"DA\n";
}

signed main()
{
    int t;
    in>>t;
    while(t--)
    {
        solve(t);
    }
    return 0;
}