Cod sursa(job #2105800)

Utilizator FrequeAlex Iordachescu Freque Data 14 ianuarie 2018 12:07:47
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.04 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>

using namespace std;

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

const int NMAX = 50000 + 5;
const int INF = 0x3f3f3f3f;

struct edge
{
    int node;
    int cost;

    edge()
    {
        node = 0;
        cost = 0;
    }

    edge(int _node, int _cost)
    {
        node = _node;
        cost = _cost;
    }

    bool operator < (const edge &arg) const
    {
        return cost < arg.cost;
    }
};

vector <edge> graph[NMAX];
priority_queue <edge> pq;
int teste, n, m, s;
int sol[NMAX], drum[NMAX];
bool vis[NMAX];

void init()
{
    for (int i = 1; i <= n; ++i)
        graph[i].clear();
    for (int i = 0; i < NMAX; ++i)
        drum[i] = INF;
    memset(vis, 0, sizeof(vis));
}

void dijkstra(int node)
{
    edge x;

    drum[node] = 0;
    pq.push(edge(node, drum[node]));
    while (!pq.empty())
    {
        x = pq.top();
        pq.pop();
        if (!vis[x.node])
        {
            vis[x.node] = true;
            for (edge i: graph[x.node])
                if (drum[i.node] > drum[x.node] + i.cost)
                {
                    drum[i.node] = drum[x.node] + i.cost;
                    if (!vis[i.node])
                        pq.push(i);
                }
        }
    }
}

bool check()
{
    for (int i = 1; i <= n; ++i)
        if (drum[i] != sol[i])
            return 0;
    return 1;
}

void write(bool ok)
{
    if (ok)
        fout << "DA" << '\n';
    else
        fout << "NU" << '\n';
}

void read()
{
    int x, y, c;

    fin >> n >> m >> s;

    for (int i = 1; i <= n; ++i)
        fin >> sol[i];

    for (int i = 1; i <= m; ++i)
    {
        fin >> x >> y >> c;
        graph[x].push_back(edge(y, c));
        graph[y].push_back(edge(x, c));
    }
}

int main()
{
    fin >> teste;
    while (teste--)
    {
        init();
        read();
        dijkstra(s);
        write(check());
    }

    return 0;
}