Cod sursa(job #1344939)

Utilizator alex_HarryBabalau Alexandru alex_Harry Data 17 februarie 2015 09:10:49
Problema Distante Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");
int N,M,D[50005],S;
vector <int> G[50005],Cost[50005];
bool Exist[50005];
void Read()
{
    f>>N>>M>>S;
    for(int i=1;i<=N;i++)
        f>>D[i];
    for(int i=1;i<=M;i++)
    {
        int x,y,c;
        f>>x>>y>>c;
        G[x].push_back(y);
        G[y].push_back(x);
        Cost[x].push_back(c);
        Cost[y].push_back(c);
    }
}

int Solve()
{
    bool ok=0;
    if(D[S]!=0)
        return 0;
    for(int node=1;node<=N;node++)
        for(int j=0;j<G[node].size();j++)
        {
            int cost=Cost[node][j],neighb=G[node][j];
            if(D[neighb]>D[node]+cost)
                return 0;
            if(D[neighb]==D[node]+cost)
                Exist[neighb]=1;
        }
    for(int i=1;i<=N;i++)
    {
        if(i==S)
            continue;
        if(Exist[i]==0)
            return 0;
    }
    return 1;
}
int main()
{
    int T;
    f>>T;
    while(T--)
    {
        Read();
        int ok=Solve();
        if(ok==1)
            g<<"DA\n";
        else
            g<<"NU\n";
        for(int i=1;i<=N;i++)
        {
            G[i].clear();
            Cost[i].clear();
        }
        memset(Exist,0,sizeof(Exist));
    }
    return 0;
}