Print? /*
Optimize O (n ^ 3) to O (n ^ 2)
The simple idea is to traverse all m points in Vt and n points in U-Vt,
After optimization, you only need to select the shortest point in the Vt.
The code is too ugly to be changed, but it is more practical.
*/
Void Prim_Alternate ()
{
Int I, f, min, j;
Vis [1] = true;
For (I = 0; I <n-1; I ++)
{
Min = inf;
For (j = 1; j <= n; j ++)
If (! Vis [j] & w [1] [j] <min)
{
Min = w [1] [j];
F = j;
}
Vis [f] = true;
Sum + = min;
For (j = 1; j <= n; j ++)
If (! Vis [j] & w [1] [j]> w [f] [j])
W [1] [j] = w [f] [j];
}
}
/*
Optimize O (n ^ 3) to O (n ^ 2)
The simple idea is to traverse all m points in Vt and n points in U-Vt,
After optimization, you only need to select the shortest point in the Vt.
The code is too ugly to be changed, but it is more practical.
*/
Void Prim_Alternate ()
{
Int I, f, min, j;
Vis [1] = true;
For (I = 0; I <n-1; I ++)
{
Min = inf;
For (j = 1; j <= n; j ++)
If (! Vis [j] & w [1] [j] <min)
{
Min = w [1] [j];
F = j;
}
Vis [f] = true;
Sum + = min;
For (j = 1; j <= n; j ++)
If (! Vis [j] & w [1] [j]> w [f] [j])
W [1] [j] = w [f] [j];
}
}