Pagini recente » Cod sursa (job #584570) | Cod sursa (job #2907074) | Cod sursa (job #1768660) | Cod sursa (job #1044114) | Cod sursa (job #2832948)
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define LSB(x) ((x) & -(x))
#define sz(x) (int)(x.size())
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define uidll(a, b) uniform_int_distribution<ll>(a, b)(rng)
typedef long long ll;
typedef long double ld;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef DEBUG
#define dbg(x...) cerr << "\e[DEBUG] "<<__func__<<": "<<__LINE__<<", [" << #x << "] = ["; _print(x);
#else
#define dbg(x...)
#endif
const char nl = '\n';
const int INF = 1e9 + 5;
const int mxN = 2e5 + 5;
const ll MOD = 1e9+7;
void testCase(){
ifstream cin("distante.in");
ofstream cout("distante.out");
int n, m, s;
cin >> n >> m >> s;
s--;
vector<vector<pair<int, int>>> al(n);
vector<int> dist(n);
for (int i = 0; i < n; i++){
cin >> dist[i];
}
for (int i = 0; i < m; i++){
int u, v, w;
cin >> u >> v >> w;
u--; v--;
al[u].eb(v, w);
al[v].eb(u, w);
}
if (dist[s] != 0){
cout << "NO\n";
return;
}
vector<bool> vis(n);
int good = 0;
queue<int> q;
q.push(s);
while (!q.empty()){
int now = q.front(); q.pop();
if (vis[now]) continue;
vis[now] = 1;
good++;
for (auto nxt : al[now]){
if (dist[nxt.f] == dist[now] + nxt.s) q.push(nxt.f);
}
}
if (good == n){
cout << "YES\n";
} else {
cout << "NO\n";
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
cin >> t;
while (t--){
testCase();
}
return 0;
}