Cod sursa(job #2604592)

Utilizator KillHorizon23Orban Robert KillHorizon23 Data 22 aprilie 2020 22:46:02
Problema Distante Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.98 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
const int VMAX = 100005;
const int INF = 1 << 30;
vector<int> D(VMAX), DNV(VMAX);
struct compare{
    bool operator()(int i, int j){
        return D[i] > D[j];}
};
void Dijkstra(int startnode, vector< pair <int, int> > graf[], int n, priority_queue<int, vector<int>, compare> q, vector<bool> inq)
{
    for (int i = 1; i <= n; ++i) D[i] = INF;
    D[startnode] = 0;
    q.push(startnode);
    inq[startnode] = true;
    while (!q.empty())
    {
        int nod = q.top();
        q.pop();
        inq[nod] = false;
        for (size_t i = 0; i < graf[nod].size(); ++i)
        {
            if (D[nod] + graf[nod][i].second < D[graf[nod][i].first])
            {
                D[graf[nod][i].first] = D[nod] + graf[nod][i].second;
                if (!inq[graf[nod][i].first])
                {
                    q.push(graf[nod][i].first);
                    inq[graf[nod][i].first] = true;
                }
            }
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(NULL), fout.tie(NULL);
    int t;
    fin >> t;
    while (t--)
    {
        vector<bool> inq(VMAX);
        vector< pair <int, int> > graf[VMAX];
        priority_queue<int, vector<int>, compare> q;
        int x, y, c;
        int n, m, s;
        fin >> n >> m >> s;
        for (int i = 1; i <= n; ++i)
            fin >> DNV[i];
        for (int i = 1; i <= m; ++i)
        {
            fin >> x >> y >> c;
            graf[x].push_back({y, c});
            graf[y].push_back({x, c});
        }
        Dijkstra(s, graf, n, q, inq);
        int nono = true;
        for (int i = 1; i <= n; ++i){
            //cout << D[i] << " ";
            if (DNV[i] != D[i]) nono = false;
        }
        //cout << "\n";
        if (!nono) fout << "NU\n";
        else fout << "DA\n";
    }
    fin.close(), fout.close();
    return 0;
}