Cod sursa(job #2916657)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 31 iulie 2022 11:53:37
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.33 kb
#include <fstream>
#include <vector>
#include <queue>
#include <stdio.h>
#include <ctype.h>

using namespace std;

#define pii pair <int,int>

class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;
	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}
public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}
	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};

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

const int max_size = 5e4 + 1, INF = 2e9 + 1;

int d[max_size], ans[max_size];
vector <pii> edges[max_size];
queue <int> coada;

void bfs (int nod)
{
    d[nod] = 0;
    coada.push(nod);
    while (!coada.empty())
    {
        nod = coada.front();
        coada.pop();
        for (auto f : edges[nod])
        {
            if (d[f.first] > d[nod] + f.second)
            {
                d[f.first] = d[nod] + f.second;
                coada.push(f.first);
            }
        }
    }
}

void solve ()
{
    int n, q, a;
    in >> n >> q >> a;
    for (int i = 1; i <= n; i++)
    {
        in >> ans[i];
        d[i] = INF;
        edges[i].clear();
    }
    while (q--)
    {
        int x, y, z;
        in >> x >> y >> z;
        edges[x].push_back({y, z});
        edges[y].push_back({x, z});
    }
    bfs(a);
    bool flag = true;
    for (int i = 1; i <= n && flag; i++)
    {
        if (d[i] != ans[i])
        {
            flag = false;
        }
    }
    if (flag)
    {
        out << "DA";
    }
    else
    {
        out << "NU";
    }
    out << '\n';
}

int main ()
{
    int t;
    in >> t;
    while (t--)
    {
        solve();
    }
    out.close();
    return 0;
}