//克鲁斯卡尔(最小生成树)
#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 ;
}

最新文章

  1. 让Git记住用户名和密码
  2. iPhone手机安全指南
  3. 转:Xms Xmx PermSize MaxPermSize 区别
  4. 图解:Arcgis Server 安装
  5. 设计模式之美:Dynamic Property(动态属性)
  6. Android 主页面顶部栏的通知Notification ,可以自定义通知消息栏的风格,并且点击通知栏进人本程序。
  7. 启动C:\Windows\System32\logiLDA.DLL时出现问题,找不到指定模块
  8. xcode7 icon图标设置
  9. &lt;memory&gt; is not a BOMStorage file
  10. 浏览器缓存相关HTTP头部字段
  11. [HNOI2008]明明的烦恼
  12. MongoDB数据创建与使用
  13. 三维计算机视觉 — 中层次视觉 — Point Pair Feature
  14. Oracle的数据并发与一致性详解(上)
  15. shopnc 手机网站配置
  16. python网络编程之UDP方式传输数据
  17. 2017-6-6&amp;6-8/大型网站架构总结
  18. .net 中 C# 简单自定义事件实现
  19. kvm 创建新虚拟机命virt-install 使用说明
  20. supervisor管理进程 superlance对进程状态报警

热门文章

  1. mysql的binlog详解
  2. mac上pydev
  3. Mac开发必备工具(二)—— iTerm 2
  4. POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )
  5. Redis实现中间件(订阅)
  6. JavaScript模板引擎使用
  7. docker hub下载慢解决方法 使用daocloud的mirror
  8. 调节音量的各个方法——AudioManager的使用
  9. Android 获取View宽度
  10. 【SDOI2012】 Longgue的问题