1004
来源:互联网 发布:linux ibus 编辑:程序博客网 时间:2024/05/21 10:33
题意:把一些镇子连起来,求连起来的最短距离
思路:显然是最下生成树问题,kruskal算法解决就可以
感想:无
源代码:
#include <iostream>
#include <fstream>#include <algorithm>
using namespace std;
const int N = 105;
int father[N];
int find(int x)
{
if (x != father[x])
father[x] = find(father[x]);
return father[x];
}
struct edge
{
int x, y, v;
}e[N*(N - 1) / 2];
int cmp(edge e1, edge e2)
{
return e1.v<e2.v;
}
int main()
{
int n;
while (cin >> n&&n)
{
for (int i = 0; i <= n; ++i)
father[i] = i;
n = n*(n - 1) / 2;
for (int i = 0; i<n; i++)
cin>>e[i].x>>e[i].y>>e[i].v;
sort(e, e + n, cmp);
int ans = 0;
for (int i = 0; i < n; ++i)
{
int x = find(e[i].x);
int y = find(e[i].y);
if (x != y)
{
ans += e[i].v;
father[x] = y;
}
}
cout << ans << endl;
}
return 0;
}
0 0
- [1004]
- 1004
- 1004
- 1004
- 1004
- 1004
- 1004
- 1004
- 1004
- 1004
- 1004
- 1004
- 1004
- 1004
- 1004
- 1004
- 1004
- 1004
- 工具的使用 —— 搜狗输入法(二)
- C++复习4.soj以及eden刷题心得
- 点 圆柱类的设计3
- LeetCode Binary Search Tree Iterator
- Android 中LayoutInflater的使用(转)
- 1004
- hjr技巧-Eclipse与MyEclipse乱码问题解决
- NSLayoutConstraint万能约束公式
- CentOs 6.5 安装OpenSSL
- android 2.x开发中actionbar的使用
- 源码阅读器的安装
- django URL模式浅析
- Python 文档处理doctest
- java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener