Cod sursa(job #2392418)

Utilizator adimiclaus15Miclaus Adrian Stefan adimiclaus15 Data 29 martie 2019 23:02:21
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

# define verf ++poz == hg ? f.read (ch, hg), poz = 0 : 0

ifstream f("distante.in");
ofstream g("distante.out");


const int hg = 1 << 13;

int  poz;
char ch[hg];

inline void cit ( int &x ) {
    if (ch[0] == '\0') f.read (ch, hg) ;
    else for (; ch[poz] < '0' || ch[poz] > '9' ; verf) ;
    for (x = 0 ; ch[poz] >= '0' && ch[poz] <= '9' ; x = x * 10 + ch[poz] - '0', verf) ;
}


int t,d[50005],n,m,s;
vector < pair<int,int> > a[50005];
string check()
{
    int i,j;
    bool sw;
    for(i=1;i<=n;i++)
    {
        if(i==s)
        {
            sw=1;
        }
        else
        {
            sw=0;
        }
        for(j=0;j<a[i].size();j++)
        {
            if(d[a[i][j].first]>d[i]+a[i][j].second)
            {
                return "NU\n";
            }
            if(i!=s)
            {
                if(d[i]==d[a[i][j].first]+a[i][j].second)
                {
                    sw=1;
                }
            }
        }
        if(sw==0)
        {
            return "NU\n";
        }
    }
    return "DA\n";
}
int main()
{
    int i,j,x,y,cost;
    cit(t);
    while(t--)
    {
        cit(n);
        cit(m);
        cit(s);
        for(i=1;i<=n;i++)
        {
            cit(d[i]);
        }
        while(m--)
        {
            cit(x);
            cit(y);
            cit(cost);
            a[x].push_back({y,cost});
            a[y].push_back({x,cost});
        }
        g<<check();
        for(i=1;i<=n;i++)
        {
            a[i].clear();
        }
    }
    return 0;
}