Pagini recente » Cod sursa (job #2382898) | Cod sursa (job #2472548) | Cod sursa (job #2530340) | Cod sursa (job #2660879) | Cod sursa (job #1643514)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("distante.in");
ofstream fout ("distante.out");
const int nmax = 50005;
const int oo= 1 << 30;
int n,m, T, c[nmax], cini[nmax],sursa;
vector < pair <int,int> > g[nmax];
priority_queue < pair<int,int>, vector < pair<int,int> >, greater < pair <int,int> > > H;
void dijkstra ()
{
while (H.size())
{
int cost_x = H.top().first;
int x= H.top().second;
H.pop();
if( cost_x <= c[x] )
{
for(int i=0; i<g[x].size(); i++ )
{
int adiacent= g[x][i].first;
int cost_adiac= g[x][i].second;
if( c[adiacent] > cost_adiac + cost_x )
{
c[adiacent]= cost_adiac + cost_x;
H.push(make_pair(c[adiacent], adiacent));
}
}
}
}
}
int main()
{
fin>>T;
while( T )
{
fin>>n>>m>>sursa;
for(int i=1; i<=n; i++)
fin>> cini[i]; /// costuri initiale
for(int i=1; i<=n; i++)
c[i]= oo;
for(int x,y,cost, i=0; i<m; i++)
{
fin>> x>>y >>cost;
g[x].push_back( make_pair(y,cost) );
g[y].push_back( make_pair(x,cost) );
}
c[sursa]= 0;
H.push( make_pair(0,sursa) ); /// (c[i],i)
dijkstra();
int ok=1;
for(int i=1; i<=n; i++)
if ( c[i]!= cini[i] ) ok =0;
if(ok==1) fout<<"DA"<<'\n';
else fout <<"NU"<<'\n';
T--;
}
return 0;
}