hdu1102 Constructing Roads 基础最小生成树
//克鲁斯卡尔(最小生成树)
#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 ;
}
hdu1102 Constructing Roads 基础最小生成树的更多相关文章
- hdu1102 Constructing Roads (简单最小生成树Prim算法)
Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...
- POJ2421 & HDU1102 Constructing Roads(最小生成树)
嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree? orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告 ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...
- POJ - 2421 Constructing Roads 【最小生成树Kruscal】
Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...
- hdu 1102 Constructing Roads (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- hdu oj1102 Constructing Roads(最小生成树)
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Constructing Roads(最小生成树)
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU1102 Constructing Roads —— 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题解: 纯最小生成树,只是有些边已经确定了要加入生成树中,特殊处理一下这些边就可以了. krus ...
- POJ 2421 Constructing Roads(最小生成树)
Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...
随机推荐
- 最简单的基于FFmpeg的AVDevice样例(读取摄像头)
=====================================================最简单的基于FFmpeg的AVDevice样例文章列表: 最简单的基于FFmpeg的AVDev ...
- 关于Widget预览图的改动
在做项目时候,由于常常不带GPS功能.所以在有些细节上须要做处理,当中之中的一个就是.快捷开关的预览图和实际效果图的差异 在我们快捷开关的预览图中,总是能够看到五个快捷开关,事实上就包含GPS信息 而 ...
- XenServer网卡Bonding
在给XenServer配置网卡bonding时,需要在所有节点都添加到集群之后再进行,这也是来自Citrix的建议:"Citrix recommends never joining a ho ...
- solr单机多实例部署文件锁冲突解决的方法
给出一个有问题的单机多tomcat实例引用同一个solr实例部署图. 这样的部署必定造成一个问题.启动第二个tomcat实例时,一定会报索引目录文件锁已经被占用. 最初的解决的方法是.有多少个tomc ...
- app上架的照片尺寸大小
- HTML canvas
什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canvas 拥有多种绘制路径.矩形.圆形.字符以 ...
- ajax异步加载问题
使用ajax异步加载数据,在之后需要用到这个数据时,应该将之后的js一并写入ajax函数中,否则后面的js不能找到动态拼接的dom节点. 或者将其封装成方法,在ajax动态加载数据的最后调用该方法.
- Java DES加密解密
import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpe ...
- (C)strcpy ,strncpy和strlcpy的基本用法
好多人已经知道利用strncpy替代strcpy来防止缓冲区越界. 但是如果还要考虑运行效率的话,也许strlcpy是一个更好的方式. 1. strcpy strcpy 是依据 /0 作为结束判断的, ...
- 5.3linux下C语言socket网络编程简例
原创文章,转载请注明转载字样和出处,谢谢! 这里给出在Linux下的简单socket网络编程的实例,使用tcp协议进行通信,服务端进行监听,在收到客户端的连接后,发送数据给客户端:客户端在接受到数据后 ...