Cod sursa(job #3380369)

Utilizator TLuca2021Teodorescu Luca Nicolae TLuca2021 Data 1 decembrie 2026 23:34:54
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.8 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
struct Edge {
    int left_node;
    int right_node;
    int weight;
};
void solve() {
    int node_count, edge_count, source_node;
    fin >> node_count >> edge_count >> source_node;
    vector<long long> proposed_distances(node_count + 1);
    for (int node = 1; node <= node_count; node++) {
        fin >> proposed_distances[node];
    }
    vector<Edge> edges(edge_count);
    for (int index = 0; index < edge_count; index++) {
        fin >> edges[index].left_node >> edges[index].right_node >> edges[index].weight;
    }
    if (proposed_distances[source_node] != 0) {
        fout << "NU\n";
        return;
    }
    vector<bool> has_optimal_predecessor(node_count + 1, false);
    has_optimal_predecessor[source_node] = true;
    for (int index = 0; index < edge_count; index++) {
        int left = edges[index].left_node;
        int right = edges[index].right_node;
        int weight = edges[index].weight;
        if (proposed_distances[left] + weight < proposed_distances[right] ||
            proposed_distances[right] + weight < proposed_distances[left]) {
            fout << "NU\n";
            return;
        }
        if (proposed_distances[left] + weight == proposed_distances[right]) {
            has_optimal_predecessor[right] = true;
        }
        if (proposed_distances[right] + weight == proposed_distances[left]) {
            has_optimal_predecessor[left] = true;
        }
    }
    for (int node = 1; node <= node_count; node++) {
        if (!has_optimal_predecessor[node]) {
            fout << "NU\n";
            return;
        }
    }
    fout << "DA\n";
}
int main() {
    int test_count;
    fin >> test_count;
    while (test_count--) {
        solve();
    }
    return 0;
}