Cod sursa(job #2830638)

Utilizator lazarstefania63@yahoo.comLazar Stefania [email protected] Data 10 ianuarie 2022 07:07:32
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.01 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;

ifstream f("graph.in");
ofstream f_out("graph.out");
vector <vector <pair <int,int>>> graph;
vector <int> dist;
vector <int> read_dist;
int test, N, M, S, x, y, z;

void add_weighted_edge(int node1, int node2, int weight)
{
    graph[node1].push_back(make_pair(node2, weight));
}

void dijkstra(int starting_node)
{
    dist.resize(N+1, INT_MAX);
    dist[starting_node] = 0;
    priority_queue <pair<int,int>> pq;
    pq.push(make_pair(-dist[starting_node], starting_node));

    while(!pq.empty())
    {
        pair<int, int> top = pq.top();
        pq.pop();
        for(int j = 0; j < graph[top.second].size(); j++)
        {
            int nod2 = graph[top.second][j].first;
            int cost = graph[top.second][j].second;
            if((-top.first) + cost < dist[nod2])
            {
                dist[nod2] = (-top.first) + cost;
                pq.push(make_pair(-dist[nod2], nod2));
            }
        }
    }
}

int main()
{
    f >> test;
    for( int i = 0; i < test; i++)
    {
        f >> N >> M >> S;
        graph.clear();
        graph.resize(N+1);
        read_dist.clear();
        read_dist.resize(N+1);
        for(int j = 1; j <= N; j++)
        {
            f >>read_dist[j];
        }
        for(int j = 0; j < M; j++)
        {
            f >> x >> y >> z;
            add_weighted_edge(x, y, z);
            add_weighted_edge(y, x, z);
        }

        dijkstra(S);

        int same_dist = 1;
        for(int j = 1; j <= N; j++)
        {
            if(dist[j] != read_dist[j])
            {
                same_dist = 0;
            }
            if(read_dist[j] == INT_MAX && dist[j] != 0)
            {
              same_dist = 0;
            }
        }

        if(same_dist == 1)
        {
            f_out << "DA" << endl;
        }
        else
        {
            f_out << "NU" << endl;
        }
    }
}