Cod sursa(job #1567313)

Utilizator Narcys01Ciovnicu Narcis Narcys01 Data 13 ianuarie 2016 09:05:59
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

#define INF (int)1e9+2

using namespace std;

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

int n, m, s;
vector <int> a[50004], b[50004];
priority_queue <int> q;
int d[50004], sol[50004];

bool Djk()
{
    int i, k, x;
    for (i = 1; i <= n; i++)
        sol[i] = INF;
    q.push(s);
    sol[s] = 0;
    while (!q.empty())
    {
        x = q.top();
        q.pop();
        k = a[x].size();
        for (i = 0; i < k; i++)
            if (sol[a[x][i]] > sol[x] + b[x][i])
            {
                sol[a[x][i]] = sol[x] + b[x][i];
                q.push(a[x][i]);
            }
    }
    for (i = 1; i <= n; i++)
        if (sol[i] != d[i])
            return false;
    return true;
}

int main()
{
    int i, T, x, y, c;
    fin >> T;
    while (T--)
    {
        fin >> n >> m >> s;
        for (i = 1; i <= n; i++)
            fin >> d[i];
        for (i = 1; i <= m; i++)
        {
            fin >> x >> y >> c;
            a[x].push_back(y);
            a[y].push_back(x);
            b[x].push_back(c);
            b[y].push_back(c);
        }
        if (Djk()) fout << "DA\n";
        else fout << "NU\n";
        for (i = 1; i <= n; i++)
        {
            a[i].erase(a[i].begin());
            b[i].erase(b[i].begin());
        }
    }
    return 0;
}