Cod sursa(job #2498686)

Utilizator RaresLiscanLiscan Rares RaresLiscan Data 24 noiembrie 2019 10:54:23
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

using namespace std;

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

struct graf {
    int nod, cost;
    bool operator < (const graf & other) const {
        return cost > other.cost;
    }
};

vector <graf> g[50005];
priority_queue <graf> q;
int dist[50005], v[50005];

void resetGraph (int n)
{
    for (int i = 1; i <= n; i ++) g[i].clear();
}

void dijkstra (int start)
{
    memset(v, INF, sizeof v);
    v[start] = 0;
    q.push({start, 0});
    while (!q.empty()) {
        int cost = q.top().cost;
        int nod = q.top().nod;
        q.pop();

        if (v[nod] != cost) continue;
        int n = g[nod].size();
        for (int i = 0; i < n; i ++) {
            int vf = g[nod][i].nod;
            if (v[vf] > cost + g[nod][i].cost) v[vf] = cost + g[nod][i].cost, q.push({vf, v[vf]});
        }
    }
}

bool verif (int n)
{
    for (int i = 1; i <= n; i ++) {
        if (v[i] != dist[i]) return false;
    }
    return true;
}

int main()
{
    int t;
    fin >> t;
    for (int j = 1; j <= t; j ++) {
        int n, m, s, a, b, c;
        fin >> n >> m >> s;
        for (int i = 1; i <= n; i ++) fin >> dist[i];
        for (int i = 1; i <= m; i ++) {
            fin >> a >> b >> c;
            g[a].push_back({b, c});
            g[b].push_back({a, c});
        }
        dijkstra(s);
        if (verif(n)) fout << "DA\n";
        else fout << "NU\n";
        resetGraph(n);
    }
    fin.close();
    fout.close();
    return 0;
}