Pagini recente » Cod sursa (job #3213090) | Monitorul de evaluare | Istoria paginii utilizator/razvanguta | Profil jolgau | Cod sursa (job #495340)
Cod sursa(job #495340)
#include <stdio.h>
#include <queue>
#include <vector>
using namespace std;
#define punct pair<int,int>
#define mkp make_pair
#define nod first
#define cos second
#define inf 2000000000
#define nmax 50005
vector < punct > G[nmax];
int t, n, m, s;
int cost[nmax], costv[nmax];
struct cmp
{
bool operator()(const int &a,const int &b) const
{
return cost[a]>cost[b];
}
};
void citire ()
{
freopen("distante.in","r",stdin);
}
void dijkstra ()
{
priority_queue<int, vector<int>, cmp> Q;
int min, i;
punct x;
for (i = 1; i <= n; i++)
cost[i] = inf;
cost[s] = 0;
Q.push(s);
while (!Q.empty() )
{
int min = Q.top (); Q.pop ();
for (i = 0; i < G[min].size (); i++)
{
x = G[min][i];
if (cost[x.nod] > cost[min]+x.cos)
{
cost[x.nod] = cost[min]+x.cos;
Q.push(x.nod);
}
}
G[min].clear ();
}
}
void afisare ()
{
bool verif = true;
int i;
for (i = 1; i <=n; i++)
if (cost[i]!=costv[i])
verif = false;
if (verif) printf("DA\n");
else printf("NU\n");
}
void solve ()
{
scanf("%d", &t);
int i, j, x, y, c;
for (i = 1; i <= t; i++)
{
scanf("%d%d%d", &n, &m, &s);
for (j = 1; j <=n; j++)
scanf("%d", &costv[j]);
for (j = 1; j <= m; j++)
{
scanf("%d%d%d", &x, &y, &c);
G[x].push_back( mkp(y,c) );
G[y].push_back( mkp(x,c) );
}
dijkstra ();
afisare ();
}
}
int main ()
{
freopen("distante.in","r",stdin);
freopen("distante.out","w",stdout);
citire ();
solve ();
return 0;
}