Pagini recente » Cod sursa (job #2255833) | Cod sursa (job #2584387) | Cod sursa (job #199126) | Cod sursa (job #2730821) | Cod sursa (job #2422702)
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
vector <pair <int,int> >graf[50005];
queue <int> Coada;
int viz[50005];
int v[50005], dist[50005];
int n, m , t, x, y,z, start, cost, nod;
ifstream f("distante.in");
ofstream g("distante.out");
int main()
{
int i = 0;
f >> t;
for(i = 1; i <= t; i++)
{
f >> n >> m >> start;
for(int j = 1; j <= n; j++)
f >> v[j];
for(int j = 1; j <= m; j++)
{
f >> x >> y >> z;
graf[x].push_back(make_pair(y, z));
graf[y].push_back(make_pair(x, z));
}
for(int j = 1; j <= n; j++)
{
dist[j] = 9999999;
viz[j] = 0;
}
dist[start] = 0;
viz[start] = 1;
Coada.push(start);
while(!Coada.empty())
{
nod = Coada.front();
viz[nod] = 0;
Coada.pop();
for(int j = 0; j < graf[nod].size(); j++)
{
x = graf[nod][j].first;
cost = graf[nod][j].second;
if(dist[x] > dist[nod] + cost)
{
dist[x] = dist[nod] + cost;
if(viz[x] == 0)
{
Coada.push(x);
viz[x] = 1;
}
}
}
}
// int ok = 0;
int j;
for( j = 1;j <= n; j++)
if(dist[j] != v[j])
break;
if(j > n)
g<<"DA"<<'\n';
else
g<<"NU"<<'\n';
for( j = 1;j <= n;++j)
graf[j].clear();
}
return 0;
}