Pagini recente » Cod sursa (job #1814114) | Cod sursa (job #236413) | Cod sursa (job #2471639) | Cod sursa (job #1622944) | Cod sursa (job #2146175)
#include <fstream>
#include <vector>
#define INF 9999999
using namespace std;
ifstream fin("distante.in");
ofstream fout("distante.out");
struct node
{
int x, cost;
node* urm;
};
typedef struct node* lsi;
lsi a[50005];
int dist[50005];
int t, n, m, s;
void read();
void solve();
void Dijkstra(int);
int main()
{
read();
return 0;
}
void read()
{
int i, j, x, y, c;
node* p;
fin >> t;
for (i = 1; i <= t; i++)
{
fin >> n >> m >> s;
for (j = 1; j <= n; j++)
a[j] = NULL;
for (j = 1; j <= n; j++)
fin >> dist[j];
for (j = 1; j <= m; j++)
{
fin >> x >> y >> c;
p = new node; p->x = y; p->cost = c; p->urm = a[x]; a[x] = p;
p = new node; p->x = x; p->cost = c; p->urm = a[y]; a[y] = p;
}
solve();
}
}
void solve()
{
if (dist[s])
{
fout << "NU\n"; return;
}
else
Dijkstra(s);
}
void Dijkstra(int start)
{
int i;
node *p;
bool ok;
for (i = 1; i <= n; i++)
if (i != s)
{
ok = false;
for (p = a[i]; p; p = p->urm)
{
if (dist[i] > dist[p->x] + p->cost)
break;
else
if (dist[i] == dist[p->x] + p->cost)
ok = true;
}
if (!ok || p)
{
fout << "NU\n";
return;
}
}
fout << "DA\n";
}