Cod sursa(job #2087719)

Utilizator Teodor.mTeodor Marchitan Teodor.m Data 14 decembrie 2017 02:23:02
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.84 kb
#include <bits/stdc++.h>
using namespace std;

FILE *in = fopen("distante.in", "r");
FILE *out = fopen("distante.out", "w");

const int Nmax = 5e4 + 50;

vector < pair < int, int > > G[Nmax];
int dist[Nmax], dist_bronzarel[Nmax];
bool viz[Nmax];

struct cmp{
    bool operator() (int a, int b){
        return dist[a] > dist[b];
    }
};

void Dijkstra(int src)
{
    priority_queue < int, vector < int >, cmp > pq;
    pq.push(src);
    viz[src] = true;

    while(!pq.empty()) {
        int node = pq.top(); pq.pop();
        viz[node] = false;

        for(auto it: G[node]) {
            if(dist[node] + it.second < dist[it.first]) {
                dist[it.first] = dist[node] + it.second;

                if(viz[it.first] == false) {
                    pq.push(it.first);
                    viz[it.first] = true;
                }
            }
        }
    }
}

int main()
{

    int T;

    fscanf(in, "%d", &T);
    for(int i = 1; i < T; ++i) {
        int n, m, src;
        fscanf(in, "%d %d %d", &n, &m, &src);

        for(int j = 1; j <= n; ++j) fscanf(in, "%d", &dist_bronzarel[j]);

        for(int j = 1; j <= n; ++j) viz[j] = false;

        for(int j = 0; j <= n; ++j)
            G[j].clear();


        for(int j = 1; j <= m; ++j) {
            int a, b, c;
            fscanf(in, "%d %d %d", &a, &b, &c);

            G[a].push_back({b, c});
            G[b].push_back({a, c});
        }

        for(int j = 1; j <= n; ++j)
            if(j != src)
                dist[j] = INT_MAX;

        Dijkstra(src);

        for(int j = 1; j <= n; ++j)
            if(dist[j] == INT_MAX)
                dist[j] = 0;

        bool ok = true;
        for(int j = 1; j <= n; ++j) {
            if(dist[j] != dist_bronzarel[j]) {
                fprintf(out, "NU\n");
                ok = false;
                break;
            }
        }

        if(ok == true)
            fprintf(out, "DA\n");
    }

    int n, m, src;
    fscanf(in, "%d %d %d", &n, &m, &src);

    for(int j = 1; j <= n; ++j) fscanf(in, "%d", &dist_bronzarel[j]);

    for(int j = 1; j <= n; ++j) viz[j] = false;

    for(int j = 0; j <= n; ++j)
        G[j].clear();


    for(int j = 1; j <= m; ++j) {
        int a, b, c;
        fscanf(in, "%d %d %d", &a, &b, &c);

        G[a].push_back({b, c});
        G[b].push_back({a, c});
    }

    for(int j = 1; j <= n; ++j)
        if(j != src)
            dist[j] = INT_MAX;

    Dijkstra(src);

    for(int j = 1; j <= n; ++j)
        if(dist[j] == INT_MAX)
            dist[j] = 0;

    bool ok = true;
    for(int j = 1; j <= n; ++j) {
        if(dist[j] != dist_bronzarel[j]) {
            fprintf(out, "NU");
            ok = false;
            break;
        }
    }

    if(ok == true)
        fprintf(out, "DA");

    return 0;
}