hdu1102 Constructing Roads 基础最小生成树
2024-08-23 04:06:17
//克鲁斯卡尔(最小生成树)
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std; const int maxn = ;
int n, t;
struct node
{
int bagin, end, len;
}arr[maxn];
int fa[maxn]; void init()
{
for (int i = ; i <= n; i++)
{
fa[i] = i;
}
} bool cmp(node a, node b)
{
return a.len<b.len;
} int find(int x)
{
if (x != fa[x])
fa[x] = find(fa[x]);
return fa[x];
} int main()
{
while (cin >> n)
{
t = ;
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
int x;
cin >> x;
arr[t].bagin = i; arr[t].end = j; arr[t].len = x;
t++;
}
}
sort(arr, arr + t, cmp);
init();
int m;
cin >> m;
while (m--)
{
int a, b;
cin >> a >> b;
int fx, fy;
fx = find(a); fy = find(b);
fa[fy] = fx;
}
int ans = ;
for (int i = ; i<t; i++)
{
int fx, fy;
fx = find(arr[i].bagin);
fy = find(arr[i].end);
if (fx != fy)
{
ans += arr[i].len;
fa[fy] = fx;
}
}
cout << ans << endl;
}
return ;
}
最新文章
- 让Git记住用户名和密码
- iPhone手机安全指南
- 转:Xms Xmx PermSize MaxPermSize 区别
- 图解:Arcgis Server 安装
- 设计模式之美:Dynamic Property(动态属性)
- Android 主页面顶部栏的通知Notification ,可以自定义通知消息栏的风格,并且点击通知栏进人本程序。
- 启动C:\Windows\System32\logiLDA.DLL时出现问题,找不到指定模块
- xcode7 icon图标设置
- <;memory>; is not a BOMStorage file
- 浏览器缓存相关HTTP头部字段
- [HNOI2008]明明的烦恼
- MongoDB数据创建与使用
- 三维计算机视觉 — 中层次视觉 — Point Pair Feature
- Oracle的数据并发与一致性详解(上)
- shopnc 手机网站配置
- python网络编程之UDP方式传输数据
- 2017-6-6&;6-8/大型网站架构总结
- .net 中 C# 简单自定义事件实现
- kvm 创建新虚拟机命virt-install 使用说明
- supervisor管理进程 superlance对进程状态报警