mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-12-24 03:31:59 +00:00
904. 虫洞
https://www.acwing.com/problem/content/submission/code_detail/7892844/
This commit is contained in:
parent
7482822f4c
commit
4370ffb409
56
AcWing/904/904.cpp
Normal file
56
AcWing/904/904.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int f, n, m, w, u, v, t, dist[505], cnt[505];
|
||||
vector<pair<int, int>> g[505];
|
||||
bool vis[505];
|
||||
|
||||
bool spfa() {
|
||||
memset(dist, 0x3f, sizeof(dist));
|
||||
memset(vis, 0x00, sizeof(vis));
|
||||
memset(cnt, 0x00, sizeof(cnt));
|
||||
queue<int> q;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
q.push(i);
|
||||
vis[i] = true;
|
||||
}
|
||||
while (!q.empty()) {
|
||||
int t = q.front();
|
||||
q.pop();
|
||||
vis[t] = false;
|
||||
for (auto i : g[t]) {
|
||||
if (dist[i.first] > dist[t] + i.second) {
|
||||
dist[i.first] = dist[t] + i.second;
|
||||
cnt[i.first] = cnt[t] + 1;
|
||||
if (cnt[i.first] >= n) return true;
|
||||
if (!vis[i.first]) {
|
||||
q.push(i.first);
|
||||
vis[i.first] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> f;
|
||||
while (f--) {
|
||||
cin >> n >> m >> w;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
g[i].clear();
|
||||
}
|
||||
for (int i = 1; i <= m; i++) {
|
||||
cin >> u >> v >> t;
|
||||
g[u].push_back(make_pair(v, t));
|
||||
g[v].push_back(make_pair(u, t));
|
||||
}
|
||||
for (int i = 1; i <= w; i++) {
|
||||
cin >> u >> v >> t;
|
||||
g[u].push_back(make_pair(v, -t));
|
||||
}
|
||||
cout << (spfa() ? "YES" : "NO") << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user