Cod sursa(job #2830500)

Utilizator lazarstefania63@yahoo.comLazar Stefania [email protected] Data 9 ianuarie 2022 23:11:08
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 8.01 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;

/*
void dfs(vector <vector <int>> graph, int starting_node, vector <int>& result)
{
    vector <int> stk;
    stk.push_back(starting_node);
    while(!stk.empty())
    {

        int node = stk[stk.size() - 1];
        int in_result = 0;
        stk.pop_back();
        for(int i = 0; i < result.size(); i++)
        {
            if(result[i] == node)
                in_result = 1;
        }
        if(in_result == 1)
        {
            continue;
        }
        result.push_back(node);
        for(auto neighbour: graph[node])
        {
            stk.push_back(neighbour);
        }
    }
}

void dfs_connected(vector <vector <int>> graph, int N, vector <vector <int>>& components)
{
    vector <int> visited, result_dfs;
    visited.resize(N+1);
    for (int nod = 1; nod <= N; nod++)
    {
        if(visited[nod] == 0)
        {
            result_dfs.clear();
            dfs(graph, nod, result_dfs);
            components.push_back(result_dfs);
            for(int j = 0; j <= result_dfs.size(); j++)
            {
                visited[result_dfs[j]] = 1;
            }
        }
    }
}

int conexidad(vector <vector <int>> graph, vector <vector <int>>& added_edges, int N, vector <vector <int>> components)
{
   vector <int> extra;
   int first_node, second_node;
   for(int i = 0; i < N; i++)
   {
       extra.push_back(0);
   }
   for (int current_index = 0; current_index < components.size() - 1; current_index++)
   {
        first_node = components[current_index][0];
        int next = components[current_index+1].size() - 1;
        second_node = components[current_index+1][next];
        extra[first_node-1] += 1;
        extra[second_node-1] += 1;
        added_edges.push_back({first_node, second_node});
   }
   return *max_element(extra.begin(), extra.end());
}

void amici_bfs(vector <vector <int>> graph, int starting_node, int& maximum, int N)
{
    queue <int> que;
    vector <int> visited;
    que.push(starting_node);
    visited.resize(N+1);
    visited[starting_node] = 1;
    while(!que.empty())
    {
        int current_node = que.front();
        que.pop();
        for(auto neighbour: graph[current_node])
        {
            if(!visited[neighbour])
            {
                visited[neighbour] = visited[current_node] + 1;
                que.push(neighbour);
                maximum = max(maximum, visited[neighbour]);
            }
        }
    }
}
bool sort_by_weight(vector <int> pair1, vector <int> pair2)
{
    return (pair1[2] > pair2[2]);
}

int transport(vector <vector <pair <int,int>>> graph, int N)
{
    int current_max, trans_min = 10001;
    vector <vector <int>> edges;
    vector <vector <int>> components;
    for(int i = 1; i <= N; i++)
    {
        for(auto edge : graph[i])
        {
            edges.push_back({i, edge.first, edge.second});
        }
    }
    sort(edges.begin(), edges.end(), sort_by_weight);
    components.push_back({edges[0][0], edges[0][1]});
    for(int i = 1; i < edges.size(); i++)
    {
        int index_first_node = -1, index_second_node = -1;
        for(int index = 0; index < components.size(); index++)
        {
            for(auto node: components[index])
            {
                if(node == edges[i][0])
                {
                    index_first_node = index;
                }
                if(node == edges[i][1])
                {
                    index_second_node = index;
                }
            }
        }
        if(index_first_node == index_second_node && index_first_node != -1)
        {
            continue;
        }
        if(index_first_node == -1 && index_second_node == -1)
        {
            //daca nu am gasit nodurile in nicio componenta conexa,
            //nodurile vor forma o noua componenta conexa
            components.push_back({edges[i][0], edges[i][1]});
            if(edges[i][2] < trans_min)
                trans_min = edges[i][2];
        }
        else if(index_first_node == -1)
        {
           components[index_second_node].push_back(edges[i][0]);
            if(edges[i][2] < trans_min)
                trans_min = edges[i][2];
        }
        else if(index_second_node == -1)
        {
            components[index_first_node].push_back(edges[i][1]);
            if(edges[i][2] < trans_min)
                trans_min = edges[i][2];
        }
        else
        {
            if(components[index_first_node].size() < components[index_second_node].size())
            {
               for(auto node: components[index_first_node])
                {
                    components[index_second_node].push_back(node);
                }
                components[index_first_node].clear();
                if(edges[i][2] < trans_min)
                trans_min = edges[i][2];
            }
            else
            {
                for(auto node: components[index_second_node])
                {
                    components[index_first_node].push_back(node);
                }
                components[index_second_node].clear();
                if(edges[i][2] < trans_min)
                trans_min = edges[i][2];
            }
        }
    }
    return trans_min;
}

void dfs_asmax(int current_node)
{
    dfs_visited[current_node] = 1;
    sums[current_node] = values[current_node];

    for(auto node : graph[current_node])
    {
        if(!dfs_visited[node])
        {
            dfs_asmax(node);
            sums[current_node] = max(sums[current_node], sums[node] + sums[current_node]);
        }
    }
}

int asmax(int N)
{
    dfs_asmax(1);
    int s_max = INT_MIN;
    for(int i = 1; i <= N; ++i)
    {
        if(sums[i] > s_max)
        {
            s_max = sums[i];
        }
    }
    return s_max;
}
*/

ifstream f("distante.in");
ofstream f_out("distante.out");
vector <vector <pair <int,int>>> graph;
vector <int> read_dist;
vector <int> dist;
int T, N, M, S, x, y, z;

/*void add_edge(int node1, int node2)
{
    graph[node1].push_back(node2);
}*/

void add_weighted_edge(int node1, int node2, int weight)
{
    graph[node1].push_back(make_pair(node2, weight));
}

void distante_dijkstra(int starting_node)
{
    dist.resize(N+1, INT_MAX);
    dist[starting_node] = 0;
    priority_queue <pair <int, int>> pq;

    pq.push(make_pair(starting_node, -dist[starting_node]));

    while(!pq.empty())
    {
        pair <int, int> t = pq.top();
        int nod = t.first;
        int min_dist = -t.second;
        pq.pop();

        for(int i = 0; i < graph[nod].size(); i++)
        {
            int nod2 = graph[nod][i].first;
            int cost = graph[nod][i].second;
            if( min_dist + cost < dist[nod2])
            {
                dist[nod2] = min_dist + cost;
                pq.push(make_pair(nod2, -dist[nod2]));
            }
        }
    }
}

int main()
{
    f >> T;
    for(int i = 1; i <= T; i++)
    {
        f >> N >> M >> S;
        graph.clear();
        read_dist.clear();
        read_dist.push_back(0);
        graph.resize(N+1);
        for(int j = 1; j <= N; j++)
        {
            f >> x;
            read_dist.push_back(x);
        }
        for(int j = 0; j < M; j++)
        {
            f >> x >> y >> z;
            add_weighted_edge(x,y,z);
            add_weighted_edge(y,x,z);
        }
        distante_dijkstra(S);
        int same_dist = 1;
        for(int j = 1; j <= N; j++)
        {
            if(read_dist[j] != dist[j])
            {
                same_dist = 0;
                break;
            }
            else if(read_dist[j]!=0 && dist[j] == INT_MAX)
            {
                same_dist = 0;
                break;
            }
        }
        if(same_dist == 1)
        {
            f_out << "DA" << endl;
        }
        else
        {
            f_out << "NU" << endl;
        }
    }
}