Cod sursa(job #2117723)

Utilizator lulian23Tiganescu Iulian lulian23 Data 29 ianuarie 2018 12:45:16
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <bits/stdc++.h>
#define pp pair <int, int>

using namespace std;

const int nmax = 5e4 + 10;
const int INF = INT_MAX;
int t, n;
int m, nod;
int verificare[ nmax ];
int dist[ nmax ];

void rezolvare(vector < pp > v[]){
    dist[ nod ] = 0;

    priority_queue < pp, vector < pp >, greater < pp > > dx;

    dx.push({0, nod});

    while (dx.empty() == false){

        int noD = dx.top().second;
        int cosT = dx.top().first;

        dx.pop();

        if (cosT > dist[ noD ])
            continue;

        for (auto it : v[ noD ]){
            if (dist[ it.first ] > cosT + it.second){
                dist[ it.first ] = cosT + it.second;
                dx.push({dist[ it.first ], it.first });
            }
        }
    }
}


int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ifstream cin("distante.in");
    ofstream cout("distante.out");

    cin >> t;

    for (int i = 0; i < t; ++i){

        cin >> n >> m >> nod;

        vector < pp > v[ n + 1 ];

        for (int j = 1; j <= n; ++j)
            cin >> verificare[ j ];

        for (int j = 0, X, Y, cost; j < m; ++j){

            cin >> X >> Y >> cost;

            v[ X ].push_back({Y, cost});
            v[ Y ].push_back({X, cost});
        }

        for (int j = 1; j <= n; ++j)
            dist[ j ] = INF;

        rezolvare( v );

        bool gut = 1;

        for (int j = 1; j <= n; ++j)
            if (dist[ j ] != verificare[ j ]){
                cout << "NU\n";
                gut = 0;
                break;
            }

        if (gut == 1)
            cout << "DA\n";
    }
}