Cod sursa(job #2245059)

Utilizator 3DwArDPauliuc Edward 3DwArD Data 24 septembrie 2018 17:46:12
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <bits/stdc++.h>
#define NMAX 50005
#define INF 0xf3f3f3
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
int nrGf;
int costInput[NMAX];
vector < pair<int, int> > graf[NMAX];
int cost[NMAX];
bitset<NMAX> used;
queue< pair<int,int> > q;
void dijkstra(){
    used.reset();
    for(int i=1;i<=NMAX;i++)
        cost[i]=INF;
    while(!q.empty()){
        int node = q.front().first;
        int prevCost = q.front().second;
        q.pop();
        for(auto it: graf[node]){
            int to = it.first;
            int toCost = it.second;
            if(cost[to]>prevCost+toCost){
                cost[to] = prevCost +toCost;
                if(used[to]==false){
                    q.push(make_pair(to,cost[to]));
                    used[to]= true;
                }
            }
        }
    }
}
void check(int n){
    for(int i=1;i<=n;i++){
        if(cost[i]==INF)cost[i]=0;
        if(costInput[i]!=cost[i]){
            g<<"NU\n";
            return;
        }
    }
    g<<"DA\n";
}
int main()
{
    f>>nrGf;
    for(int k=1;k<=nrGf;k++){
        for(auto it: graf)
        while(!it.empty())it.pop_back();
        int n,m,sursa,from,to,_cost;
        f>>n>>m>>sursa;
        for(int i=1;i<=n;i++)f>>costInput[i];
        for(int i=1;i<=m;i++){
            f>>from>>to>>_cost;
            graf[from].push_back(make_pair(to, _cost));
            graf[to].push_back(make_pair(from, _cost));
        }
        q.push(make_pair(sursa, 0));
        used[sursa]=1;
        dijkstra();
        cost[sursa]=0;
        check(n);
    }
    return 0;
}