Cod sursa(job #993633)

Utilizator acomAndrei Comaneci acom Data 4 septembrie 2013 11:01:00
Problema Distante Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include<cstdio>
#include<vector>
using namespace std;
#define inf 1<<30
struct muchie {int x,y,c;} G[50005];
int t,m,n,s,a[50005],d[50005];
bool egal()
{
    int i;
    for (i=1;i<=n;++i)
        if (a[i]!=d[i]) return false;
    return true;
}
void rez()
{
    int x,y,c,i;
    scanf("%d%d%d",&n,&m,&s);
    for (i=1;i<=n;++i) scanf("%d",&a[i]), d[i]=inf;
    d[s]=0;
    for (i=1;i<=m;++i)
        {
            scanf("%d%d%d",&x,&y,&c);
            G[i].x=x, G[i].y=y, G[i].c=c;
            if (x==s) d[y]=c;
            if (y==s) d[x]=c;
        }
    bool ok=false;
    while (!ok)
        {
            ok=true;
            for (i=1;i<=m;++i)
                {
                    x=G[i].x, y=G[i].y;
                    if (d[y]>d[x]+G[i].c)
                        {
                            d[y]=d[x]+G[i].c;
                            ok=false;
                        }
                }
        }
    if (egal()) printf("DA\n");
    else printf("NU\n");
}
int main()
{
    int k;
    freopen("distante.in","r",stdin);
    freopen("distante.out","w",stdout);
    scanf("%d",&t);
    for (k=0;k<t;++k)
        rez();
    return 0;
}