Cod sursa(job #2366424)

Utilizator YediuTeZediu Almos-Agoston YediuTe Data 4 martie 2019 20:01:00
Problema Distante Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.72 kb
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
#define MAX_INT 99999

using namespace std;

ifstream f("distante.in");
ofstream g("distante.out");

int T;
int N, M, S;

void solve(int N, int M, int S)
{
    S--;
    vector<int> givenDist;
    vector<int> dist(N, MAX_INT);
    vector< pair<int, int> > emptyVec;
    vector< vector< pair<int, int> > > graph;
    for(int i = 0; i < N; i++){
        graph.push_back(emptyVec);
        int val;
        f >> val;
        givenDist.push_back(val);
    }
    for(int i = 0; i < M; i++)
    {
        int weight, from, to;
        f >> from >> to >> weight;
        from--, to--;
        graph[from].push_back(make_pair(to, weight));
        graph[to].push_back(make_pair(from, weight));
    }

    set< pair<int,int> > minSet;
    minSet.insert(make_pair(0, S));
    dist[S] = 0;
    while(!minSet.empty()){
        pair<int,int> temporary = *(minSet.begin());
        minSet.erase(minSet.begin());
        int currentNode = temporary.second;
        for(int i = 0; i < graph[currentNode].size(); i++){
            int node = graph[currentNode][i].first;
            int weight = graph[currentNode][i].second;
            if(dist[node] > dist[currentNode] + weight){
                if(dist[node] == MAX_INT)
                    minSet.erase(make_pair(dist[node], node));
                dist[node] = dist[currentNode] + weight;
                minSet.insert(make_pair(dist[node], node));
            }
        }
    }
    if(givenDist == dist)
        g << "DA" << endl;
    else
        g << "NU" << endl;
}

int main()
{
    f >> T;
    for(int i = 0; i < T; i++){
        f >> N >> M >> S;
        solve(N,M,S);
    }
}