Cod sursa(job #2971274)

Utilizator Luka77Anastase Luca George Luka77 Data 26 ianuarie 2023 22:27:06
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.05 kb
#include <bits/stdc++.h>
#define FOR(WHATEVER) for(int i = 1; i <= WHATEVER; ++ i)
using namespace std;

/// INPUT / OUTPUT
const string problem = "distante";
ifstream fin(problem + ".in");
ofstream fout(problem + ".out");

/// STRUCTURES
struct muchie
{
    int dest, cost;
};

struct priority
{
    int dest, cost;
    bool operator < (const priority &num) const
    {
        return cost > num.cost;
    }
};

/// GLOBAL VARIABLES
const int NMAX = 50005, MOD = 1e9 + 7, INF = 1e9;
int tt;

/// SOLUTION
inline void solution()
{
    int n, m, s;
    int bronz[NMAX], viz[NMAX], dist[NMAX];
    vector<muchie>graph[NMAX];
    memset(dist, 0, sizeof(dist));
    memset(bronz, 0, sizeof(bronz));
    memset(viz, 0, sizeof(viz));
    fin >> n >> m >> s;
    for(int i = 1; i <= n; ++ i)
        fin >> bronz[i];
    for(int i = 1; i <= m; ++ i)
    {
        int node1, node2, cost;
        fin >> node1 >> node2 >> cost;
        graph[node1].push_back({node2, cost});
        graph[node2].push_back({node1, cost});
    }
    for(int i = 1; i <= n; ++ i)
        dist[i] = INF;
    dist[s] = 0;
    priority_queue<priority>pq;
    pq.push({s, 0});
    while(!pq.empty())
    {
        int curr_node = pq.top().dest;
        pq.pop();
        if(viz[curr_node])
            continue;
        viz[curr_node] = 1;
        for(auto new_node : graph[curr_node])
        {
            //cout << dist[new_node.dest] << '\n';
            if(dist[new_node.dest] > dist[curr_node] + new_node.cost)
            {
                dist[new_node.dest] = dist[curr_node] + new_node.cost;
                pq.push({new_node.dest, dist[new_node.dest]});
            }
        }
    }
    for(int i = 1; i <= n; ++ i)
    {
        if(dist[i] != bronz[i])
        {
            fout << "NU" << '\n';
            return;
        }
    }
    fout << "DA" << '\n';
}

/// READING THE INPUT
int main()
{
    ios::sync_with_stdio(false);
    fin.tie(NULL);
    fout.tie(NULL);

    fin >> tt;
    while(tt--)
        solution();
}