Pagini recente » Cod sursa (job #1479390) | Cod sursa (job #2458213) | Cod sursa (job #353677) | Cod sursa (job #377615) | Cod sursa (job #2400680)
#include<fstream>
#include<vector>
#include<queue>
#define NMAX 50010
#define INF 1e9
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
vector<pair<int,int>>G[NMAX];
int main()
{
int t;
fin >> t;
for(int i = 0; i < t; i++)
{
int n, m, k;
fin >> n >> m >> k;
int ans[n+3]={0};
for(int j = 1; j <= n; j++)
{
fin >> ans[j];
}
vector<int>dist(n+2,INF);
dist[k] = 0;
for(int j = 0; j < m; j++)
{
int x, y, cost;
fin >> x >> y >> cost;
G[x].push_back(make_pair(y, cost));
G[y].push_back(make_pair(x, cost));
}
priority_queue<pair<int,int>>q;
q.push(make_pair(0,k));
bool viz[n+3]={0};
while(!q.empty())
{
int x = q.top().second;
q.pop();
if(!viz[x])
{
viz[x] = true;
for(unsigned int k = 0; k < G[x].size(); k++)
{
if(dist[x] + G[x][k].second < dist[G[x][k].first])
{
dist[G[x][k].first] = dist[x] + G[x][k].second;
q.push(make_pair( -dist[G[x][k].first], G[x][k].first));
}
}
}
}
bool ok = 1;
for(int j = 1; j <= n; j++)
{
if(dist[j] != ans[j])
{
ok = 0;
break;
}
}
if(ok == 1)
{
fout << "DA\n";
}
else
{
fout << "NU\n";
}
for(int j = 1; j <= n; j++)
{
G[j].clear();
}
}
fin.close();
fout.close();
return 0;
}