Cod sursa(job #3263507)

Utilizator drsbosDarius Scripcaru drsbos Data 14 decembrie 2024 16:59:32
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.75 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[100001];//nod,cost
int x, y, T, n, m,c,a[100001],dist[100001],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 });
        }
        dj(s);
        int vf = 0;
        for (int i = 1; i <= n && !vf; i++)
            if (dist[i] != a[i])
            {
                fout << "NU\n";
                    vf = 1;
            }
        if(vf==0)
        fout << "DA\n";
        for (int i = 1; i <= n; i++)
            l[i].clear();
    }
}