Cod sursa(job #3272645)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 30 ianuarie 2025 17:01:59
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.95 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 5e4 + 5;
vector<pair<int, int>> adj[NMAX+1];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;

int dist[NMAX+1];

void clear(){
    
    for(int i=1; i<=NMAX; i++)
        dist[i] = 1e9;
        
    for(int i=1; i<=NMAX; i ++)
        adj[i].clear();
        
    while(!pq.empty())
        pq.pop();
    
}

int vrf[NMAX+1];

int n, m, s;

void dijkstra(int src){
    
    dist[src] = 0;
    pq.push({0, src});
    
    while(!pq.empty()){
        
        int fromcost = pq.top().first;
        int fromnod = pq.top().second;
        
        pq.pop();
        
        if(dist[fromnod] < fromcost)
            continue;

            
        for(auto next : adj[fromnod]){
            
            int to = next.second;
            int tocost = next.first;
            
            if(dist[to] > dist[fromnod] + tocost){
                
                
                dist[to] = dist[fromnod] + tocost;
                pq.push({dist[to], to});
                
            }
            
        }
        
    }
    
    for(int j=1; j<=n; j++){
        
        if(dist[j] != vrf[j]){
            
            g << "NU" << "\n";
            return ;
            
        }
        
    }
    
    
    g << "DA" << "\n";
    
}

int main()
{
    
    int t;
    f >> t;
    
    for(int i=1; i<=t; i++){
        
     
        f >> n >> m >> s;
        
        clear();
        
        for(int j=1; j<=n; j++)
            f >> vrf[j];
        
        for(int j=1; j<=m; j++){
            
            int x, y, cost;
            f >> x >> y >> cost;
            
           // cout << x <<  ' ' << y << ' ' << cost << endl;
            
            
            adj[x].push_back({cost, y});
            adj[y].push_back({cost, x});
            
        }
        
        dijkstra(s);
        
    }
    
    
   

    return 0;
}