版本比较
图论:最小生成树
正文
增加 12 行删除 12 行
51 行未改动52535455565657585960606162636364656667686970717272737475757677787928 行未改动1081091101111121121131141141151161171183 行未改动1221231241251261261271281291307 行未改动1381391401411421421431441451461471481491501501511521531549 行未改动16416516616716816816917017117117217317417527 行未改动51 行未改动
int main() { ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m; for (int i = 1; i <= m; ++i) cin >> e[i].u >> e[i].v >> e[i].w;
sort(e + 1, e + 1 + m, cmp); //对边按边权从小到大排序 for (int i = 1; i <= n; ++i) p[i] = i; //并查集初始化
for (int i = 1; i <= m; ++i) { int u = get(e[i].u), v = get(e[i].v); if (u != v) { p[u] = v; ans += e[i].w; --n; } }
if (n > 1) cout << "orz"; else cout << ans;
return 0;}```
28 行未改动 int x, y, z; cin >> x >> y >> z; w[x][y] = w[y][x] = min(w[x][y], z); }
memset(d, 0x3f, sizeof d); d[1] = 0;
for (int i = 1; i <= n; i++) { int u = 0; for (int j = 1; j <= n; j++) if (!vis[j] && d[u] > d[j]) u = j;3 行未改动 for (int j = 1; j <= n; j++) d[j] = min(d[j], w[u][j]); } cout << ans;
return 0;}```
7 行未改动vector<pair<int, int>> G[N];int main() { ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m; while (m--) { int u, v, w; cin >> u >> v >> w; G[u].push_back({v, w}); G[v].push_back({u, w}); }
memset(d, 0x3f, sizeof d); priority_queue<pair<int, int>> q; q.push({0, 1}); d[1] = 0;9 行未改动 q.push({-d[v], v}); } } }
if (n) cout << "orz"; else cout << ans;
return 0;}
```27 行未改动