Cod sursa(job #3221421)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 7 aprilie 2024 08:43:26
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.88 kb
#ifndef LOCAL
    #pragma GCC optimize("O3")
    #pragma GCC target("avx2")
#endif

#ifdef LOCAL
    #define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
using ll = long long;
using ci = const int;
using cll = const long long;

using namespace std;

const int NMAX = 5e4 + 5;
const int INF = 1e8 + 5;
/*******************************/
// INPUT / OUTPUT

#ifndef LOCAL
    ifstream in("distante.in");
    ofstream out("distante.out");
    #define cin in
    #define cout out
#endif
/*******************************/
/// GLOBAL DECLARATIONS

int T;
int N, M, S;
bool ok;
bool vis[NMAX];
int dist[NMAX], rasp[NMAX];

struct Item
{
    int node, cost;

    const bool operator < (const Item &other) const
    {
        if (cost == other.cost)
            return node > other.node;
        return cost > other.cost;
    }
};

vector <Item> adj[NMAX];
priority_queue <Item> pq;
/*******************************/
/// FUNCTIONS

void ReadInput();
void Solution();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
    cin >> T;
}
///-------------------------------------
inline void Reset()
{
    ok = true;
}
///-------------------------------------
inline void Read()
{
    cin >> N >> M >> S;

    for (int i = 1 ; i <= N ; ++ i)
        adj[i].clear();
    
    for (int i = 1 ; i <= N ; ++ i)
        cin >> dist[i];
    
    int a, b, cost;
    for (int i = 0 ; i < M ; ++ i)
    {
        cin >> a >> b >> cost;

        adj[a].push_back({b, cost});
        adj[b].push_back({a, cost});
    }
}
///-------------------------------------
inline void Solve()
{
    for (int i = 1 ; i <= N ; ++ i)
    {
        vis[i] = false; rasp[i] = INF;
    }

    int node, cost;
    pq.push({S, 0});
    while (!pq.empty())
    {
        node = pq.top().node;
        cost = pq.top().cost;
        pq.pop();

        if (!vis[node])
        {
            vis[node] = true;
            rasp[node] = cost;

            for (auto u: adj[node])
            {
                if (!vis[u.node] && rasp[u.node] > cost + u.cost)
                {
                    rasp[u.node] = cost + u.cost;
                    pq.push({u.node, cost + u.cost});
                }
            }
        }
    }

    for (int i = 1 ; i <= N ; ++ i)
        if (rasp[i] != dist[i])
            ok = false;
}
///-------------------------------------
inline void Output()
{
    if (ok) cout << "DA\n";
    else cout << "NU\n";
}
///-------------------------------------
inline void TestCase()
{
    Reset();
    Read();
    Solve();
    Output();
}
///-------------------------------------
inline void Solution()
{
    while (T --)
    {
        TestCase();
    }
}
///-------------------------------------
int main()
{
    #ifndef LOCAL
        ios::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
    #endif
    ReadInput();
    Solution();
    return 0;
}