O(n²+m)
int g[N][N], dist[N];//memset(g, 0x3f, sizeof g);
bool s[N];
int prim()//连通时返回最小生成树边权和,否则返回INF(0x3f3f3f3f)
{
memset(dist, 0x3f, sizeof(dist));
int res = 0;
for (int i = 0; i < n; i++)
{
int t = -1;
for (int j = 1; j <= n; j++)
{
if (!s[j] && (t == -1 || dist[t] > dist[j]))
{
t = j;
}
}
if (i && dist[t] == INF)
{
return INF;
}
if (i)
{
res += dist[t];
}
for (int j = 1; j <= n; j++)
{
dist[j] = min(dist[j], g[t][j]);
}
s[t] = 1;
}
return res;
}