Seek common method negative ring based SPFA:
① number of statistics for each point into the team, if the team a point n times, then there is a negative ring.
② side of the current shortest path statistics for each point contained, if the number of edges in the shortest path comprises a point greater than equal to n, then there is a negative ring.
The second method is usually used.
1 bool spfa() 2 { 3 memset(dist, 0, sizeof dist); 4 memset(cnt, 0, sizeof cnt); 5 memset(st, 0, sizeof st); 6 7 int hh = 0, tt = 0; 8 for (int i = 1; i <= n; i ++ ) 9 { 10 q[tt ++ ] = i; 11 st[i] = true; 12 } 13 14 while (hh != tt) 15 { 16 int t = q[hh ++ ]; 17 if (hh == N) hh = 0; 18 st[t] = false; 19 20 for (int i = h[t]; ~i; i = ne[i]) 21 { 22 int j = e[i]; 23 if (dist[j] > dist[t] + w[i]) 24 { 25 dist[j] = dist[t] + w[i]; 26 cnt[j] = cnt[t] + 1; 27 if (cnt[j] >= n) return true; 28 if (!st[j]) 29 { 30 q[tt ++ ] = j; 31 if (tt == N) tt = 0; 32 st[j] = true; 33 } 34 } 35 } 36 } 37 38 return false; 39 }
\frac{\sum_{i = 1}^{n}f_{i}}{\sum_{i = 1}^{n}t_{i}} > Mid