最小生成树
作者:互联网
Prim O(mlogm)
memset(dis, 80, sizeof(dis));
q.push({ 1,0 });
dis[1] = 0;
while (!q.empty())
{
int u = q.top().s;
q.pop();
if (vis[u])continue;
vis[u] = true, ++num, ans += dis[u];
for (int i = hd[u]; i; i = g[i].nxt)
{
int v = g[i].to, w = g[i].val;
if (dis[v] > w)
{
dis[v] = w;
q.push({ v,dis[v] });
}
}
}
Kruskal O(mlogm)
sort(g + 1, g + m + 1);
for (int i = 1; i <= m; i++)
{
int fu = find(g[i].u);
int fv = find(g[i].v);
if (fu == fv)continue;
ans += g[i].val;
fa[v] = u;
cnt++;
}
标签:int,memset,最小,mlogm,vis,push,生成,dis 来源: https://www.cnblogs.com/xqk0225/p/16303760.html