Cod sursa(job #2718395)

Utilizator rarestudurTudur Rares rarestudur Data 8 martie 2021 18:31:11
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <fstream>
#include <queue>
#include <vector>
using namespace std;

const int N = 50001;
const int INF = 1e9;
int n,d[N],dist[N];
bool inq[N],este_corect = true;

vector <pair<int,int>> a[N];
queue <int> q;

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

void bellmanford(int s)
{
    d[s] = 0;
    inq[s] = true;
    q.push(s);
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        inq[x] = false;
        for(auto p:a[x])
        {
            int y = p.first;
            int c = p.second;
            if(d[y] > d[x] + c)
            {
                d[y] = d[x] + c;
                if(!inq[y])
                {
                    q.push(y);
                    inq[y] = true;
                }
            }
        }
    }
}

int main()
{
    int T;
    in >> T;
    for(int i = 1; i <= T; i++)
    {
        int n,m,start;
        in >> n >> m >> start;
        for(int i = 1; i <= n; i++)
        {
            in >> dist[i];
        }
        for(int i = 1; i <= m; i++)
        {
            int x,y,c;
            in >> x >> y >> c;
            a[x].push_back({y,c});
            a[y].push_back({x,c});
        }
        for(int i = 1; i <= n; i++)
        {
            d[i] = INF;
        }
        bellmanford(start);
        for(int i = 1; i <= n; i++)
        {
            if(d[i] != dist[i])
            {
                out << "NU" << "\n";
                este_corect = false;
                break;
            }
        }
        if(este_corect)
        {
            out << "DA" << "\n";
        }
    }
    return 0;
}