Cod sursa(job #2526016)

Utilizator MichaelXcXCiuciulete Mihai MichaelXcX Data 18 ianuarie 2020 10:38:55
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 100005;
const int INF = 0x3f3f3f;

int T, N, M, S, a, b, c;
int Dist[NMAX], InitialDist[NMAX], checked[NMAX];

struct Edge {
    int next;
    int cost;
};

vector< Edge > G[NMAX];
queue< int > Q;

void init() {
    for(int i = 1; i <= N; i++) {
        Dist[i] = INF;
        checked[i] = 0;
        G[i].clear();
    }
}

void Dijkstra()
{
    Q.push(S);
    Dist[S] = 0;
    checked[S] = 1;

    while(!Q.empty()) {
        int currentNode = Q.front();
        Q.pop();

        for(auto &m : G[currentNode]) {
            if(Dist[currentNode] + m.cost < Dist[m.next]) {
                Dist[m.next] = Dist[currentNode] + m.cost;

                if(!checked[m.next])
                    Q.push(m.next);

                checked[m.next] = 1;
            }
        }
        checked[currentNode] = 0;
    }
}

void Solution()
{
    fin >> N >> M >> S;
    init();
    for(int i = 1; i <= N; ++i)
        fin >> InitialDist[i];

    for(int i = 0; i < M; ++i) {
        fin >> a >> b >> c;

        G[a].push_back({b, c});
        G[b].push_back({a, c});
    }

    Dijkstra();
    Dist[S] = 0;

    for(int i = 1; i <= N; ++i) {
        if(Dist[i] != InitialDist[i]) {
            fout << "NU\n";
            return;
        }
    }
    fout << "DA\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(NULL);

    fin >> T;

    while(T--) {
        Solution();

        //Dijkstra(S);
        //(isMatch()) ? fout << "DA\n" : fout << "NU\n";
    }
}