/* Find the loop with the smallest average weight.
Here there is a conversion. W1 + W2 +... + Wk <k * mid;
The average weight of the binary loop mid, converts the above formula to (W1-mid) + (W2-mid) +... + (Wk-mid) <0;
Spfa is used to determine whether a negative weight loop exists. Note that it is a negative weight loop.
If mid = max (Wi) + 1 does not exist, there is no solution.
Because mid is the maximum possible value, and no negative weight loop is available at this time, it will not be available when mid gets a smaller value. */
[Cpp]
# Include <stdio. h>
# Include <cstring>
# Include <algorithm>
# Include <queue>
Using namespace std;
Const int maxm = 3001;
Const int maxn = 51;
Struct edge
{
Int next,;
Double w;
} E [maxm];
Int t;
Bool vis [maxn];
Int head [maxn], num [maxn];
Double d [maxn];
Void add (int I, int j, double w)
{
E [t]. to = j;
E [t]. w = w;
E [t]. next = head [I];
Head [I] = t ++;
}
Int n, m;
Bool spfa ()
{
Queue <int> q;
Memset (num, 0, sizeof (num ));
Memset (vis, false, sizeof (vis ));
Memset (d, 0x7f, sizeof (d ));
For (int I = 1; I <= n; I ++) vis [I] = true, d [I] = 0, q. push (I );
While (! Q. empty ())
{
Int u = q. front ();
Q. pop ();
Vis [u] = false;
For (int I = head [u]; I! =-1; I = e [I]. next)
{
Int v = e [I].;
If (d [u] + e [I]. w <d [v])
{
D [v] = d [u] + e [I]. w;
If (! Vis [v])
{
Vis [v] = true;
Q. push (v );
If (++ num [v]> n) return true;
}
}
}
}
Return false;
}
Bool work (double x)
{
For (int I = 0; I <m; I ++)
E [I]. w-= x;
Bool ret = spfa ();
For (int I = 0; I <m; I ++)
E [I]. w + = x;
Return ret;
}
Int main ()
{
// Freopen ("B .txt", "r", stdin );
Int T, a, B, test = 1;
Double w;
Scanf ("% d", & T );
While (T --)
{
Scanf ("% d", & n, & m );
Double temp = 0;
T = 0;
Memset (head,-1, sizeof (head ));
For (int I = 0; I <m; I ++)
{
Scanf ("% d % lf", & a, & B, & w );
Add (a, B, w );
Temp = max (temp, w );
}
Printf ("Case # % d:", test ++ );
If (! Work (temp + 1 ))
{
Printf ("No cycle found. \ n ");
Continue;
}
Double l = 0, r = temp, mid;
While (r-l> 1e-3)
{
Mid = (l + r)/2;
If (work (mid) r = mid;
Else l = mid;
}
Printf ("%. 2lf \ n", l );
}
Return 0;
}
# Include <stdio. h>
# Include <cstring>
# Include <algorithm>
# Include <queue>
Using namespace std;
Const int maxm = 3001;
Const int maxn = 51;
Struct edge
{
Int next,;
Double w;
} E [maxm];
Int t;
Bool vis [maxn];
Int head [maxn], num [maxn];
Double d [maxn];
Void add (int I, int j, double w)
{
E [t]. to = j;
E [t]. w = w;
E [t]. next = head [I];
Head [I] = t ++;
}
Int n, m;
Bool spfa ()
{
Queue <int> q;
Memset (num, 0, sizeof (num ));
Memset (vis, false, sizeof (vis ));
Memset (d, 0x7f, sizeof (d ));
For (int I = 1; I <= n; I ++) vis [I] = true, d [I] = 0, q. push (I );
While (! Q. empty ())
{
Int u = q. front ();
Q. pop ();
Vis [u] = false;
For (int I = head [u]; I! =-1; I = e [I]. next)
{
Int v = e [I].;
If (d [u] + e [I]. w <d [v])
{
D [v] = d [u] + e [I]. w;
If (! Vis [v])
{
Vis [v] = true;
Q. push (v );
If (++ num [v]> n) return true;
}
}
}
}
Return false;
}
Bool work (double x)
{
For (int I = 0; I <m; I ++)
E [I]. w-= x;
Bool ret = spfa ();
For (int I = 0; I <m; I ++)
E [I]. w + = x;
Return ret;
}
Int main ()
{
// Freopen ("B .txt", "r", stdin );
Int T, a, B, test = 1;
Double w;
Scanf ("% d", & T );
While (T --)
{
Scanf ("% d", & n, & m );
Double temp = 0;
T = 0;
Memset (head,-1, sizeof (head ));
For (int I = 0; I <m; I ++)
{
Scanf ("% d % lf", & a, & B, & w );
Add (a, B, w );
Temp = max (temp, w );
}
Printf ("Case # % d:", test ++ );
If (! Work (temp + 1 ))
{
Printf ("No cycle found. \ n ");
Continue;
}
Double l = 0, r = temp, mid;
While (r-l> 1e-3)
{
Mid = (l + r)/2;
If (work (mid) r = mid;
Else l = mid;
}
Printf ("%. 2lf \ n", l );
}
Return 0;
}