Cod sursa(job #2423374)

Utilizator dragos99Homner Dragos dragos99 Data 21 mai 2019 11:38:15
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.28 kb
#include<fstream>
#include<vector>
#define NMAX 50005
using namespace std;
    ifstream f("distante.in");
    ofstream g("distante.out");

int n, m, s, ok, d[NMAX], t;

vector < pair<int,int> > graf[NMAX];


void verifica()
{
    for(int i = 1 ; i <= n ; i++){
        if (i != s){
            int ok = 0;
            for(int j = 0 ; j < graf[i].size() ; j++){
                if(d[graf[i][j].first] > d[i] + graf[i][j].second){
                    g<<"NU"<<'\n';
                    return ;
                }
                if(d[i] == d[graf[i][j].first] + graf[i][j].second)
                    ok = 1;
            }
            if(!ok){
                g<<"NU"<<'\n';
                return ;
            }
        }
    }
    g<<"DA"<<'\n';
    return ;
}

void citire()
{
    f>>t;
    for(int i = 0 ; i < t ; i++){
        f>>n>>m>>s;
        for(int j = 1 ; j <= n ; j++){
            f>>d[j];
        }
        for(int j = 0 ; j < m ; j++){
            int x, y, c;
            f>>x>>y>>c;
            graf[x].push_back(make_pair(y, c));
            graf[y].push_back(make_pair(x, c));
        }
        if(d[s])
            g<<"NU"<<'\n';
        else{
            verifica();
        }
    }
}

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