#include <cstdio> #include <cstring> #include <queue> using namespace std; const int maxn = 55; const int maxm = maxn * maxn; const double INF = 50.0 * 10000000.0; const double EPS = 1e-8; class CNode { public: int x, next; double w; }; int n, m; int len, h[maxm]; CNode table[maxm]; int cnt[maxn]; double dis[maxn]; double mid, l, r; bool vis[maxn], Qvis[maxn]; queue<int> Q; inline void (int u, int v, int w) { len++; table[len].x = v; table[len].w = w; table[len].next = h[u]; } inline bool spfa(int s, double x) { int p; memset(cnt, 0, sizeof(cnt)); memset(Qvis, 0, sizeof(Qvis)); for (int i = 1; i <= n; i++) dis[i] = INF; while (!Q.empty()) Q.pop(); Q.push(s); dis[s] = 0; vis[s] = true; Qvis[s] = true; cnt[s] = 1; while (!Q.empty()) { p = Q.front(); Q.pop(); Qvis[p] = false; for (int i = h[p]; i; i = table[i].next) { if (dis[table[i].x] > dis[p] + (table[i].w - x)) { dis[table[i].x] = dis[p] + (table[i].w - x); if (!Qvis[table[i].x]) { vis[table[i].x] = true; Qvis[table[i].x] = true; Q.push(table[i].x); cnt[table[i].x]++; if (cnt[table[i].x] > n) return true; } } } } return false; } inline bool check(double x) { memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) if (!vis[i] && spfa(i, x)) return true; return false; } inline void solve(int tcase) { int u, v, w; len = 0; memset(h, 0, sizeof(h)); printf("Case #%d: ", tcase); scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d%d", &u, &v, &w); add(u, v, w); } l = 0; r = INF; while (l < r) { mid = (l + r) / 2.0; if (check(mid)) r = mid - EPS; else l = mid + EPS; } if (fabs(l - INF) < EPS) printf("No cycle found.n"); else printf("%.2fn", l); } int main() { int T; scanf("%d", &T); for (int tcase = 1; tcase <= T; tcase++) solve(tcase); return 0; }
|