HDU 1879 继续畅通工程——最小生成树

网友投稿 637 2022-11-29

HDU 1879 继续畅通工程——最小生成树

HDU 1879 继续畅通工程——最小生成树

#include #include #include #include #include using namespace std;const int maxn = 110;int par[maxn], ran[maxn];void init(int n) { for (int i = 0; i <= n; i++) { par[i] = i, ran[i] = 0; }}int seek(int x) { if (par[x] == x) return x; else return par[x] = seek(par[x]);}void unite(int x, int y) { x = seek(x), y = seek(y); if (x == y) return; if (ran[x] == ran[y]) par[x] = y; else { par[y] = x; if (ran[x] == ran[y]) ran[x]++; }}struct Edge { int u, v; double cost; bool operator < (const Edge &another) const { return cost < another.cost; }}edge[maxn*maxn];int main(){ int n, u, v, cost, flag; while (scanf("%d", &n) == 1 && n) { int len = n * (n - 1) / 2; int edge_cnt = 0, cnt = 0; init(n); for (int i = 1; i <= len; i++) { scanf("%d %d %d %d", &u, &v, &cost, &flag); if (flag == 1) { if (seek(u) != seek(v)) { cnt++; unite(u, v); } } else if (flag == 0) { edge_cnt++; edge[edge_cnt].u = u, edge[edge_cnt].v = v, edge[edge_cnt].cost = cost; } } sort(edge + 1, edge + 1 + edge_cnt); int ans = 0; for (int i = 1; i <= edge_cnt; i++) { if (cnt == n - 1) break; if (seek(edge[i].u) != seek(edge[i].v)) { cnt++; ans += edge[i].cost; unite(edge[i].u, edge[i].v); } } printf("%d\n", ans); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Ireport的安装与使用教程
下一篇:HDU 2544 最短路——dijlstra
相关文章

 发表评论

暂时没有评论,来抢沙发吧~