Cod sursa(job #3319149)

Utilizator Mihaita09Nechitescu Mihai Mihaita09 Data 30 octombrie 2025 18:32:14
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <bits/stdc++.h>

using namespace std;

#define cin fin
#define cout fout
ifstream fin("distante.in");
ofstream fout("distante.out");

struct el{
int nod, cost;
bool operator < (const el &other) const{
    return cost > other.cost;
}
};

priority_queue<el> pq;
vector<el> a[100001];
int n,m,t,start,distante[100001],dis[100001];
bool ok, viz[100001];

void dijkstra(int start)
{
    dis[start]=0;
    for (int i = 1; i <= m; i++)
    {
        if (i!=start) dis[i] = 1e9;
    }
    pq.push({start,0});
    while (!pq.empty())
    {
        el x = pq.top();
        pq.pop();
        if (!viz[x.nod]){
            viz[x.nod] = 1;
            for (auto y:a[x.nod])
            {
                if (!viz[y.nod]){
                dis[y.nod] = min(dis[y.nod],y.cost + x.cost);
                pq.push({y.nod,dis[y.nod]});
            }
        }
    }
    }
}

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {

        ok=1;
        cin >> m >> t >> start;
        for (int i = 1; i <= m; i++)
        {
            cin >> distante[i];
        }
        for (int i = 1; i <= t; i++)
        {
            int x,y,cost;
            cin >> x >> y >> cost;
            a[x].push_back({y,cost});
            a[y].push_back({x,cost});
        }
        dijkstra(start);
        for (int i = 1; i <= m; i++)
        {
            if (dis[i] != distante[i]) ok=0;
        }
        for (int i = 1; i <= m; i++)
        {
            distante[i] = 0;
            viz[i] = 0;
            a[i].clear();
        }
        if (ok == 0) cout << "NU";
        else cout << "DA";
    }



}