Cod sursa(job #3263509)

Utilizator drsbosDarius Scripcaru drsbos Data 14 decembrie 2024 17:03:07
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <fstream>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <set>
#include <cstring>
#include <map>
#include <string>
#include<iomanip>
#include<bitset>
#define oo 2000000000
#define MOD 777013
using namespace std;

ifstream fin("distante.in");
ofstream fout("distante.out");
priority_queue<pair<int, int>, vector <pair<int, int> >, greater <pair<int, int>>> pq;///dist,nod
vector<pair<int, int>>l[50004];//nod,cost
int x, y, T, n, m,c,a[50004],dist[50004],s;

void dj(int start)
{
    for (int i = 1; i <= n; i++)
        dist[i] = oo;
    dist[start] = 0;
    pq.push({ 0,start });
    while (!pq.empty())
    {
        int currcost = pq.top().first;
        int nod = pq.top().second;
        pq.pop();
        if (currcost > dist[nod])continue;
        for (auto next : l[nod])
        {
            if (dist[next.first] > currcost + next.second)
            {
                dist[next.first] = currcost + next.second;
                pq.push({ dist[next.first],next.first });
            }
        }
    }
}

int main()
{
    fin >> T;
    for (int pas = 1; pas <= T; pas++)
    {
        fin >> n >> m >> s;
        for (int i = 1; i <= n; i++)
            fin >> a[i];
        for (int i = 1; i <= m; i++)
        {
            fin >> x >> y >> c;
            l[x].push_back({ y,c });
            l[y].push_back({ x,c });
        }
        dj(s);
        bool ok = 1;
        for (int i = 1; i <= n; i++)
            if (dist[i] != a[i])
                ok = false;
        if (ok)
            fout << "DA\n";
        else
            fout << "NU\n";
        for (int i = 1; i <= n; i++)
            l[i].clear();

    }
}