Cod sursa(job #2105027)

Utilizator bucuralexandraioana05Bucur Alexandra bucuralexandraioana05 Data 12 ianuarie 2018 15:28:07
Problema Distante Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.98 kb
#include <iostream>
#include<bits/stdc++.h>

using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
#define oo 100000000
#define lim 50001
struct nod
{
    int NC;
    int cost;
};
vector <nod> G[lim];
priority_queue <pair <int,int> > q;

int n,m,nrg,start;
int raspuns[lim], rez[lim], viz[lim];
int i,j;
int a,b,c;
int ok;
int t;
pair<int,int> aux;
int main()
{
    fin>>nrg;
    while(nrg)
    {
        t=1;
        fin>>n>>m>>start;
        for(j=1;j<=n;j++)
                {fin>>raspuns[j];
                if(j==start)
                   { if(raspuns[start]!=0)
                          {fout<<"NU"<<'\n'; t=0;}
                   }
                if (t==1)
                {
                 rez[j]=oo;
                 viz[j]=0;
                 G[j].clear();
                }
                }
        if(t==0)
           for(j=1;j<=m;j++)
                 fin>>a>>b>>c;
       else
        {
            for(j=1;j<=m;j++)
            {
                fin>>a>>b>>c;
                G[a].push_back({b,c});
                G[b].push_back({a,c});
            }
        rez[start]=0;
        q.push({0,start});
        while(!q.empty())
        {
            aux=q.top();
            q.pop();
            if( !viz[aux.second])
            {
                for(auto p:G[aux.second])
                {
                    if( rez[p.NC]>p.cost+rez[aux.second])
                        rez[p.NC]=p.cost+rez[aux.second];
                    q.push({-rez[p.NC],p.NC});
                }
                viz[aux.second]=1;
            }
        }
        ok=1;
        for(j=1;j<=n && ok==1; j++)
        {
            if(rez[j]!=oo)
                {if(rez[j]!=raspuns[j]) ok=0;}
            else   ///rez[j]==oo
              if(raspuns[j]!=0) ok=0;
        }

         if(ok) fout<<"DA"<<'\n';
         else fout<<"NU"<<'\n';
        }
     nrg--;
    }
    fin.close();
    fout.close();

    return 0;
}