Cod sursa(job #2397496)

Utilizator Teodor01Dorde Teodor Teodor01 Data 4 aprilie 2019 14:34:00
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.72 kb
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int const NM = 1e5 / 2;
struct code {
    int node , cost;
    bool operator > (code r) const {
        return cost > r . cost;
    }
};
vector <code> v [1 + NM];
int dp [1 + NM] , dp2 [1 + NM];
priority_queue <code , vector <code> , greater <code> > q;
bool viz [1 + NM];
ifstream cin("distante.in");
ofstream cout("distante.out");
void shortest (int nodes , int last){
    q . push ({last , 0});
    while (! q . empty ()){
        last = q . top () . node;
        int cost = q . top () . cost;
        viz [last] = true;
        while (q . size () && viz [q . top () . node])
            q . pop ();
        for(auto i : v [last])
            if (dp [i . node] > dp [last] + i . cost){
                dp [i . node] = dp [last] + i . cost;
                q . push ({i . node , dp [i . node]});
            }
    }
}
bool check (int n){
    for(int i = 1 ; i <= n ; ++ i)
        if (dp [i] != dp2 [i])
            return false;
    return true;
}
int main (){
    int t , n , m , s;
    cin>>t;
    while (t --){
        cin>>n>>m>>s;
        fill (dp + 1 , dp + n + 1 , (1 << 30));
        dp [s] = 0;
        for(int i = 1 ; i <= n ; ++ i)
            f >> dp2 [i];
        for (int i = 1 ; i <= n ; ++ i)
            v [i] . clear ();
        for(int i = 1 ; i <= m ; ++ i){
            int a , b , c;
            f >> a >> b >> c;
            v [a] . push_back ({b , c});
            v [b] . push_back ({a , c});
        }
        fill (viz + 1 , viz + n + 1 , false);
        shortest (n , s);
        if (check (n))
            cout<<"DA\n";
        else
            cout<<"NU\n";
    }
    return 0;
}