-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path48.spfa_negative_ring.cpp
More file actions
54 lines (53 loc) · 926 Bytes
/
Copy path48.spfa_negative_ring.cpp
File metadata and controls
54 lines (53 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 2e3 + 10, M = 1e4 + 10;
int n, m;
int h[N], e[M], ne[M], w[M], idx;
bool st[N];
int dist[N];
int cnt[N];
void add(int a, int b, int c){
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
bool spfa(){
queue<int> q;
for(int i = 1; i <= n; i++){
q.push(i);
st[i] = true;
}
while(q.size()){
int t = q.front();
q.pop();
st[t] = false;
for(int i = h[t]; i != -1; i = ne[i]){
int j = e[i];
if(dist[j] > dist[t] + w[i]){
dist[j] = dist[t] + w[i];
cnt[j] = cnt[t] + 1;
if(cnt[j] >= n) return true;
if(!st[j]){
q.push(j);
st[j] = true;
}
}
}
}
return false;
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof h);
for(int i = 0; i < m; i++){
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
if(spfa()){
cout << "Yes" << endl;
}
else cout << "No" << endl;
return 0;
}