Cod sursa(job #1994552)

Utilizator moonlightVartic Petru-Alexandru moonlight Data 25 iunie 2017 12:41:17
Problema Distante Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
#include <cstring>
#define dim 100005
#define INF 0x3f3f3f3f
using namespace std;
vector<pair <int,int> >G[dim];
int n,m,x,y,c,a,t,start_node,dist[dim],dist1[dim];

void read();
int dijkstra(int nod);

int main()
{
    read();
    return 0;
}


void read(){

    fstream fin("distante.in",ios::in);
    fstream fout("distante.out",ios::out);
    fin >> t;
    for(int i=1;i<=t;++i){
        bool oke = true;
        fin >> n >> m >> start_node;

        for(int j=1;j<=n;++j)
            fin >> dist[j];

        for(int j=1;j<=m;++j){
            fin >> x >> y >> c;
            G[x].push_back(make_pair(y,c));
            G[y].push_back(make_pair(x,c));
        }

        dijkstra(start_node);

        for(int j=1;j<=n;++j)
            G[j].erase(G[j].begin(),G[j].end());

        for(int j=1;j<=n;j++)
            if(dist1[j] != dist[j]){
                oke = false;
                break;
            }
        if(oke)
            fout << "DA\n";
        else
            fout << "NU\n";
    }

    fin.close();
    fout.close();
}


int dijkstra(int nod){

    int cont = 1;
    set<pair <int,int> >Set;
    Set.insert(make_pair(0,nod));
    memset(dist1,INF,sizeof(dist1));
    dist1[nod] = 0;

    while(!Set.empty()){

        int node = Set.begin()->second;
        int distance = Set.begin()->first;
        Set.erase(Set.begin());

        for(vector<pair<int,int> >::iterator it = G[node].begin(); it != G[node].end();++it){
            int y = it->first;
            int c = it->second;

            if(dist1[y] > dist1[node] + c){

                if(dist1[y] != INF)
                    Set.erase(Set.find(make_pair(dist1[y],y)));
                dist1[y] = dist1[node] + c;
                Set.insert(make_pair(dist1[y],y));
            }
        }
    }
}