Cod sursa(job #2830261)

Utilizator AndreeaCreitaCreita Andreea AndreeaCreita Data 9 ianuarie 2022 17:58:41
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.13 kb
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
ifstream in("distante.in");
ofstream out("distante.out");
const int INF = 1e9;

vector<pair<int,int>> la[50005];
int dist[50005];
int dist_bronz[50005];
int viz[50005];
int n,m,s,t;
void dijkstra()
{
    dist[s] = 0;                              //distanta la nodul initial e 0
    priority_queue <pair<int,int>> pq;        //pereche ce contine nodul catre care se duce (to) si costul muchiei
    pq.push({0, s});                          //nodul initial are costul 0

    while (pq.size())
    {
        int from = pq.top().second;

        pq.pop();
        if(viz[from])
            continue;
        viz[from] = 1;
        for (auto& per: la[from])             //ia fiecare pereche din lista de adi
        {
            int to = per.first;               //to e muchia la care se duce
            int cost = per.second;            //cost = costul muchiei

            if (dist[to] > dist[from] + cost)
            {
                dist[to] = dist[from] + cost; //distanta se actualizeaza cu suma dintre dist la from so costul de la from la to
                pq.push({-dist[to], to});     //pune in ordine crescatoare
            }
        }
    }
}
int main()
{
    in >> t;
    for (int i = 1; i <= n; i++)
        la[i].clear();
    while (t > 0)
    {
        in >> n >> m >> s;
        for (int i = 1; i <= n; i++)
        {
            int c;
            in >> c;
            dist_bronz[i] = c;
        }
        for (int i = 1; i <= n; i++)
        {
            viz[i] = 0;
            dist[i] = INF;
        }

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

            int x,y,d;    //from, to, costul
            in >> x >> y >> d;
            la[x].push_back({y,d});
            la[y].push_back({x,d});
        }

        dijkstra();

        int ok = 1;
        for (int i = 1; i <= n; i++)
        {
            if (dist_bronz[i] != dist[i])
            {
                ok--;
                out << "NU"<<endl;

            }
        }
        if(ok) out << "DA"<<endl;

        t--;
    }

    return 0;
}