The two-point answer M, and then the entire edge minus m, if there is a negative circle, then the mean smaller circle exists. Negative circles are judged by DFS.
---------------------------------------------------------------------------
#include <bits/stdc++.h>#define REP (i, n) for (int i = 0; i < n; ++i)#define CLR (x, C) memset (x, C, sizeof (x))#define foreach (i, x) for (__typeof (X.begin ()) i = X.begin (); I! = X.end (); i++)using namespace std; const int MAXN = 3009;Const DOUBLE EPS = 1e-9;struct Edge {int to;Double Dist, W;};vector<edge> G[MAXN];int n;double D[MAXN];bool VIS[MAXN], F;void Dfs (int x) {vis[x] = true;foreach (E, g[x]) if (D[e->to] > D[x] + e->w) {if (!vis[e->to]) {D[e->to] = d[x] + e->w;DFS (e->to);} elseF = true;if (F) break;}Vis[x] = false;} bool Check (double m) {Rep (i, N) {foreach (it, g[i]) it->w = it->dist-m;Vis[i] = False, d[i] = 0;}F = false;Rep (i, N) {DFS (i);if (F) return true;}return false;}int main () {freopen ("test.in", "R", stdin);int m;cin >> n >> m;Rep (i, N) g[i].clear ();While (m--) {int u, v;double D;scanf ("%d%d%lf", &u, &v, &d); u--, v--;G[u].push_back (Edge) {V, D, 0});}double L = -1e7, R = 1e7;While (R-l > EPS) {Double m = (L + R)/2;check (m)? R = M:l = m;}printf ("%.8lf\n", L);return 0;}
---------------------------------------------------------------------------
1486: [HNOI2009] min loop time limit: Sec Memory Limit: MB
Submit: 1447 Solved: 679
[Submit] [Status] [Discuss] Descriptioninputoutputsample Inputsample Outputhint
Source
Bzoj 1486: [HNOI2009] Minimum Circle (two-point answer + DFS award negative circle)