Cod sursa(job #3002702)

Utilizator samyro14Samy Dragos samyro14 Data 14 martie 2023 23:57:54
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <bits/stdc++.h>

using namespace std;
#define fastread ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
ifstream fin("distante.in");
ofstream fout("distante.out");
const int maxn = 5e4;
int t, n, m, start;
int d[maxn + 2];
int dis[maxn + 2];
vector<pair<int, int>> a[maxn + 2];
priority_queue<pair<int, int>> q;
void solve(){
    fin >> n >> m >> start;
    bool ok = true;
    for(int i = 1; i <= n; ++i) fin >> d[i], a[i].clear(), dis[i] = 1e8;
    for(int i = 1; i <= m; ++i){
        int x, y, cost; fin >> x >> y >> cost;
        a[x].push_back({y, cost});
        a[y].push_back({x, cost});
    }
    dis[start] = 0;
    q.push({0, start});
    while(!q.empty()){
        int nod = q.top().second;
        q.pop();
        for(auto& x : a[nod]){
            int cost2 = x.second;
            int y = x.first;
            if(dis[y] > dis[nod] + cost2){
                dis[y] = dis[nod] + cost2;
                q.push({-dis[y], y});
            }
        }
    }
    for(int i = 1; i <= n; ++i)
        if(dis[i] != d[i]){
            fout << "NU \n";
            return;
        }
    fout << "DA \n";


}
int main(){
    fin >> t;
    while(t) solve(), t--;    
    return 0;
}
/*


 */