剖析虛幻渲染體系(06)- UE5特輯Part 1(特性和Nanite)
阿新 • • 發佈:2021-06-24
Kruskal
前置:並查集
struct node { ll u, v, w; } t[200005]; ll fa[200005], n, m, ans, eu, ev, cnt; inline bool cmp(node a, node b) { return a.w < b.w; } inline ll find(ll x) { if (fa[x] == x) return x; return fa[x] = find(fa[x]); } inline void K() { sort(t, t + m, cmp); for (int i = 1; i <= m; ++i) { eu = find(t[i].u), ev = find(t[i].v); if (eu == ev) continue; ans += t[i].w; fa[ev] = eu; if (++cnt == n) break; } } int main() { n = read(); m = read(); for (int i = 1; i <= n; ++i) fa[i] = i; for (int i = 1; i <= m; ++i) t[i].u = read(), t[i].v = read(), t[i].w = read(); K(); printf("%lld\n", ans); Edison Ba; }
Prim
int ans, cnt, now = 1; //Prim void prim() { for (int i = 2; i <= n; ++i) dis[i] = MAXN; for (int i = head[1]; i; i = t[i].nxt) dis[t[i].to] = min(dis[t[i].to], t[i].w); while (++cnt < n) { int minn = MAXN; vis[now] = 1; for (int i = 1; i <= n; ++i) { if (vis[i]) continue; if (minn > dis[i]) { minn = dis[i]; now = i; } } ans += minn; for (int i = head[now]; i; i = t[i].nxt) { int y = t[i].to, z = t[i].w; if (vis[y]) continue; if (dis[y] > z) { dis[y] = z; } } } }