Cod sursa(job #2372394)

Utilizator SirVSbiVidam Szablocs SirVSbi Data 7 martie 2019 09:02:59
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.16 kb
#include <fstream>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>

#define INF 1 << 30
#define MAX 50001

using namespace std;

int main(){

    ifstream be("distante.in");
    ofstream ki("distante.out");

    int t;
    be >> t;
    while(t){
        int n, m, s, from, to, cost;
        int dist[MAX];
        vector<pair <int, int>> graph[MAX];
        bool ok = true;
        be >> n >> m >> s;
        int distante[n];
        for(int i = 1; i <= n; i++){
            be >> distante[i];
            dist[i] = INF;
        }
        dist[s] = 0;

        for(int i = 0; i < m; i++){
            be >> from >> to >> cost;
            graph[from].push_back(make_pair(to, cost));
            graph[to].push_back(make_pair(from, cost));
        }
        set<pair<int, int>> heap;
        heap.insert(make_pair(0, s));
        while(!heap.empty()){
            int node = heap.begin() -> second;

            heap.erase(heap.begin());
            for(vector<pair<int, int>>::iterator it = graph[node].begin(); it != graph[node].end(); it++){
                to = it -> first;
                cost = it -> second;
          //      cout << to << " " << cost << endl;
                if(dist[to] > dist[node] + cost){
                    if(dist[to] != INF){
                        heap.erase(heap.find(make_pair(dist[to], to)));
                    }
                    dist[to] = dist[node] + cost;
                    heap.insert(make_pair(dist[to], to));
                }
            }
            for(int i = 1; i <= n; i++){
            if(dist[i] != distante[i] and dist[i] != INF){
                ok = false;
                break;
            }
        }
        }
/*
        for(int i = 1; i <= n; i++){
            cout << dist[i] << " ";
        }
        cout << endl;

        for(int i = 1; i <= n; i++){

            cout << distante[i] << " ";
        }
        cout << endl;
*/

        for(int i = 1; i <= n; i++){
            if(dist[i] != distante[i]){
                ok = false;
                break;
            }
        }
        if(ok){
            ki << "DA" << endl;
        }
        else{
            ki << "NU" << endl;
        }
        t--;
    }


}