Pagini recente » Cod sursa (job #2713553) | Cod sursa (job #1983613) | Cod sursa (job #2954275) | Cod sursa (job #2572381) | Cod sursa (job #2117793)
#include <fstream>
#include <vector>
#include <queue>
#include <cctype>
#include <bitset>
#define N 50003
#define BUF_SIZE 1000000
#define oo 1e9
using namespace std;
char buf[BUF_SIZE];
int pos = BUF_SIZE;
FILE *fr,*fw;
inline char getChar()
{
if(pos == BUF_SIZE)
{
fread(buf,1,BUF_SIZE,fr);
pos = 0;
}
return buf[pos++];
}
inline int Read()
{
int result =0;
char c;
do
{
c = getChar();
}while(!isdigit(c));
do
{
result = result*10 + c -'0';
c = getChar();
}while(isdigit(c));
return result;
}
int T,n,m,s;
int x,y,c;
int dist[N],D[N];
vector<pair<int,int> > v[N];
priority_queue<pair<int,int> > q; //nod, -cost ( cost minim )
bitset<N> f;
inline int Dijkstra()
{
for(int i=1; i<=n; ++i)
v[i].clear();
f.reset();
n = Read(); m = Read(); s = Read();
for(int i=1; i<=n; ++i)
D[i] = Read();
while(m--)
{
x = Read(); y = Read(); c = Read();
v[x].push_back(make_pair(y,c));
v[y].push_back(make_pair(x,c));
}
if(D[s] != 0) return 0;
for(int i=1; i<=n; ++i)
dist[i] = oo;
dist[s] = 0;
q.push(make_pair(s,0));
while(!q.empty())
{
int nod = q.top().first;
int costurm,nodurm;
q.pop();
if(f[nod]) continue;
f[nod] = 1;
for(int i=0; i!=v[nod].size(); ++i)
{
nodurm = v[nod][i].first;
costurm = v[nod][i].second;
if(dist[nodurm] > dist[nod] + costurm)
{
dist[nodurm] = dist[nod] + costurm;
q.push(make_pair(nodurm,-dist[nodurm]));
}
}
}
for(int i=1; i<=n; ++i)
if(dist[i] != D[i]) return 0;
return 1;
/*for(int i=1; i<=n; ++i)
fprintf(fw,"%d ",dist[i]);
fprintf(fw,"\n");*/
}
int main()
{
fr = fopen("distante.in","r");
fw = fopen("distante.out","w");
T = Read();
while(T--)
fprintf(fw,(Dijkstra() ? "DA\n":"NU\n"));
fclose(fr); fclose(fw);
return 0;
}