Pagini recente » Cod sursa (job #658982) | Cod sursa (job #1570301) | Cod sursa (job #1127636) | Cod sursa (job #1023546) | Cod sursa (job #2796990)
#include <stdio.h>
#include <queue>
#include <vector>
#include <ctype.h>
#define MAX_N 50000
using namespace std;
struct muchie {
int nod, cost;
bool operator < (const muchie &aux) const {
return cost > aux.cost;
}
};
FILE *fin;
int dist[MAX_N], distCer[MAX_N];
vector <muchie> muchii[MAX_N];
priority_queue <muchie> pq;
int getNum() {
char ch;
int n;
ch = getc( fin );
while ( isspace( ch ) && ch != EOF )
ch = getc( fin );
n = 0;
while ( isdigit( ch ) ) {
n = n * 10 + ch - '0';
ch = getc( fin );
}
return n;
}
int main() {
FILE *fout;
int t, n, m, s, x, y, c, i;
struct muchie crt, next;
fin = fopen( "distante.in", "r" );
fout = fopen( "distante.out", "w" );
fscanf( fin, "%d", &t );
while ( t-- ) {
n = getNum();
m = getNum();
s = getNum() - 1;
for ( i = 0; i < n; i++ ) {
distCer[i] = getNum();
dist[i] = -1;
muchii[i].clear();
}
for ( i = 0; i < m; i++ ) {
x = getNum() - 1;
y = getNum() - 1;
c = getNum();
muchii[x].push_back( { y, c } );
muchii[y].push_back( { x, c } );
}
pq.push( { s, 0 } );
while ( !pq.empty() ) {
crt = pq.top();
pq.pop();
if ( dist[crt.nod] == -1 ) {
dist[crt.nod] = crt.cost;
for ( i = 0; i < muchii[crt.nod].size(); i++ ) {
next = muchii[crt.nod][i];
if ( dist[next.nod] == -1 )
pq.push( { next.nod, dist[crt.nod] + next.cost } );
}
}
}
i = 0;
while ( i < n && dist[i] == distCer[i] )
i++;
if ( i == n )
fprintf( fout, "DA\n" );
else
fprintf( fout, "NU\n" );
}
fclose( fin );
fclose( fout );
return 0;
}