WA on #1 #9
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define p pair<int,int>
vector<bool>inq; vector<int> d, cnt; vector<vector<p > >m;
bool spfa(int f, int num) {
queue<p> q;
inq[f] = 1;
q.push({ 0,f });
d[f] = 0;
while (!q.empty()) {
int point = q.front().second, value = q.front().first;
q.pop();
inq[point] = 0;
if (cnt[point] > num)return false;
for (int i = 0; i < m[point].size(); i++) {
int n_v = m[point][i].second + value, n_p = m[point][i].first;
if (n_v < d[n_p]) {
d[n_p] = n_v;
if (inq[n_p] == 0) {
cnt[n_p] ++;
inq[n_p] = 1;
q.push({ d[n_p],n_p });
if (cnt[point] > num)return false;
}
}
}
}
return true;
}
int main() {
int t;
cin >> t;
for (int l = 0; l < t; l++) {
int i, j, s=1;
cin >> i >> j ;
inq.resize(0);
cnt . resize(0);
d.resize(0);
m.resize(0);
inq.resize(3007);
cnt.resize(3007);
d.resize(3007, 1 << 25);
m.resize(3007);
for (int k = 0; k < j; k++) {
int x, y, z;
cin >> x >> y >> z;
//有向边
if (z < 0)m[x].push_back({ y,z }); else m[x].push_back({ y,z }), m[y].push_back({ x,z });
}
if (!spfa(1, i))cout << "YES"<<endl;
else cout << "NO"<<endl;
}
}