Cod sursa(job #3221471)

Utilizator Victor_9Diaconescu Victor Victor_9 Data 7 aprilie 2024 11:25:42
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.13 kb
//SIMULARE ONI ANDREI distante -- 
#include <bits/stdc++.h>
using namespace std;
const int nmax = 50005;
const int inf = 1e9 + 7;

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

int t, n, m, s, v[nmax], vis[nmax], ok;
int dist[nmax];
int x, y, z;

typedef struct poz{
    int nod;
    int cost;
}student;
vector < student > adj[nmax];

struct coord{
    int node, cost;
    
    const bool operator <(const coord &other)const{
        if (cost == other.cost){
            return node > other.node;
        }
        return cost > other.cost;
    }
};
priority_queue < coord > pq;

int main()
{
    ios_base::sync_with_stdio(0);
    fin.tie(0) , fout.tie(0);
    
    fin>>t;
    
    while(t){
        
        fin>>n>>m>>s;
        for(int i=1;i<=n;i++){
            fin>>v[i];
        }
        
        for(int i=1;i<=m;i++){
            fin>>x>>y>>z;
            adj[x].push_back({y , z});
            adj[y].push_back({x , z});
        }
        
        //Dijkstra
        
        for(int i=1;i<=n;i++){
            dist[i] = inf;
            vis[i] = 0;
        }
        
        dist[s] = 0;
        pq.push({s , 0});
        
        while(!pq.empty()){
            int node = pq.top().node , pret = pq.top().cost;
            pq.pop();
            
            if(vis[node] == 1) continue;
            
            vis[node] = 1;
            
            for(auto u : adj[node]){
                if(vis[u.nod] == 0 && dist[u.nod] > dist[node] + u.cost){
                    dist[u.nod] = dist[node] + u.cost;
                    pq.push({u.nod , dist[u.nod]});
                }
                
            }
            
            
        }
        
        ok = 0;
        
        for(int i=1;i<=n;i++){
            if(v[i] != dist[i]){
                ok = 1;
                break;
            }
        }
        
        if(ok == 1){
            fout<<"NU";
        }else{
            fout<<"DA";
        }
        
        if(t!=1){
            fout<<"\n";
        }
        
        //reset
        for(int i=1;i<=n;i++){
            adj[i].clear();
        }
        
        t --;
    }
    
    
    
    
    
    
    
    
    
    
}