0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 20:58:49 +00:00
OI-codes/Luogu/P3385/P3385.cpp

80 lines
1.4 KiB
C++
Raw Normal View History

#include <iostream>
#include <algorithm>
#include <queue>
#include <utility>
#include <vector>
2021-07-22 16:35:48 +00:00
using std::cin;
using std::cout;
const char endl = '\n';
2021-07-22 16:35:48 +00:00
const int N = 2e3 + 5;
const int INF = 0X3f3f3f3f;
2021-07-22 16:35:48 +00:00
int t, n, m, dist[N], cnt[N];
std::vector<std::pair<int, int>> g[N];
bool vis[N];
2021-07-22 16:35:48 +00:00
bool spfa() {
std::fill_n(cnt, N, 0);
std::fill_n(vis, N, false);
std::fill_n(dist, N, INF);
std::queue<int> q;
q.emplace(1);
dist[1] = 0;
vis[1] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for (auto e : g[u]) {
int v = e.first,
w = e.second;
2022-05-20 01:23:27 +00:00
if (dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
if (!vis[v]) {
cnt[v] = cnt[u] + 1;
if (cnt[v] >= n) return false;
vis[v] = true;
q.emplace(v);
}
}
}
2021-07-22 16:35:48 +00:00
}
return true;
}
2021-07-22 16:35:48 +00:00
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
2021-07-22 16:35:48 +00:00
cin >> t;
2021-07-22 16:35:48 +00:00
while (t--) {
cin >> n >> m;
for (int i = 1, u, v, w; i <= m; i++) {
2021-07-22 16:35:48 +00:00
cin >> u >> v >> w;
g[u].emplace_back(v, w);
if (w >= 0) g[v].emplace_back(u, w);
2021-07-22 16:35:48 +00:00
}
cout << (!spfa() ? "YES" : "NO") << endl;
2021-07-22 16:35:48 +00:00
for (int i = 1; i <= n; i++) {
g[i].clear();
2021-07-22 16:35:48 +00:00
}
}
2021-07-22 16:35:48 +00:00
return 0;
}