Pagini recente » Clasament antrenamentoji | Cod sursa (job #2998327) | Cod sursa (job #456383) | Cod sursa (job #669576) | Cod sursa (job #2932930)
#include <fstream>
#include <vector>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
using pereche=pair<int, int>;
ifstream fin("distante.in");
ofstream fout("distante.out");
const int dim=5e4+5;
int T;
void dijkstra(int src, int d[],int v[], vector<pereche> g[])
{
priority_queue<pereche, vector<pereche>, greater<pereche> >pq;
d[src]=0;
pq.push({d[src], src});
while(!pq.empty())
{
int cost=pq.top().first;
int nod=pq.top().second;
pq.pop();
if(!v[nod])
{
v[nod]=1;
for(auto &i: g[nod])
{
int nodNou=i.first;
int costNou=i.second;
if(d[nodNou]>d[nod]+costNou)
{
d[nodNou]=d[nod]+costNou;
pq.push({d[nodNou], nodNou});
}
}
}
}
}
int main()
{
fin>>T;
for(int i=1; i<=T; i++)
{
int n, m, s, nod;
fin>>n>>m>>s;
int dDat[dim];
vector<pereche>g[dim];
for(int i=1; i<=n; i++)
{
fin>>dDat[i];
if(!dDat[i])
nod=i;
}
int x, y, c;
for(int i=1; i<=m; i++)
{
fin>>x>>y>>c;
g[x].push_back({y, c});
g[y].push_back({x, c});
}
int d[dim];
for(int i=1; i<=n; i++)
if(i!=nod)
d[i]=inf;
int v[dim]={0};
bool ok=true;
dijkstra(nod, d, v, g);
for(int i=1; i<=n; i++)
if(i!=nod)
{
if(d[i]!=dDat[i])
ok=false;
}
if(ok)
fout<<"DA"<<'\n';
else fout<<"NU"<<'\n';
}
fin.close();
fout.close();
return 0;
}