//克鲁斯卡尔(最小生成树)
#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 基础最小生成树的更多相关文章

  1. hdu1102 Constructing Roads (简单最小生成树Prim算法)

    Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...

  2. POJ2421 & HDU1102 Constructing Roads(最小生成树)

    嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree?   orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告 ...

  3. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

  4. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

    Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...

  5. hdu 1102 Constructing Roads (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  6. hdu oj1102 Constructing Roads(最小生成树)

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. Constructing Roads(最小生成树)

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  8. HDU1102 Constructing Roads —— 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题解: 纯最小生成树,只是有些边已经确定了要加入生成树中,特殊处理一下这些边就可以了. krus ...

  9. POJ 2421 Constructing Roads(最小生成树)

    Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...

随机推荐

  1. openwrt: sysupgrade

    sysupgrade 用法: sysupgrade [...] sysupgrade [-q] [-i] 升级选项: -d 重启前等待 delay 秒 -f 从 .tar.gz (文件或链接) 中恢复 ...

  2. [网页游戏开发]进一步了解Morn UI及工作流

    Morn UI工作流 Morn Builder不仅仅是对Flash IDE的改进,传统的开发协作是以fla为基础,由于fla是二进制文件,在以svn等版本控制软件协作下,合并过程中会出现各种各样的问题 ...

  3. Preference+PreferenceArray+DataModel

    在Mahout中,用户的喜好被抽象为一个Preference,包含了userId,itemId和偏好值(user对item的偏好).Preference是一个接口,它有一个通用的实现是GenericP ...

  4. C++虚复制构造函数,设置Clone()方法返回基类指针,并设置为虚函数

    构造函数不能是虚函数.但有时候确实需要能传递一个指向基类对象的指针,并且有已创建的派生类对象的拷贝.通常在类内部创建一个Clone()方法,并设置为虚函数. //Listing 12.11 Virtu ...

  5. SpringMvc參数的接受以及serializeArray的使用方法

    需求:从页面提交一个table中的数据到后台,通经常使用于批量改动 把全部的数据到放到 input属性中,设置name定义成为对象的相关属性,使用Jquery的serializeArray这种方法封装 ...

  6. node-sass 安装失败win32-x64-48_binding.node

    升级了nodejs的版本,原项目的node-sass模块启动安装不了. 下载对应的win32-x64-xx_binding.node https://github.com/sass/node-sass ...

  7. mini_magick

    https://github.com/minimagick/minimagick class  https://www.rubydoc.info/github/minimagick/minimagic ...

  8. HDU2612 Find a way —— BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 MS (Java/Others ...

  9. Do not throw System.Exception, System.SystemException, System.NullReferenceException, or System.IndexOutOfRangeException intentionally from your own source code

    sonarqube的扫描结果提示 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/exceptions/creatin ...

  10. Python小练习_将数据库中表数据存到redis里

    # ##练习:将xxx数据库中my_user表中数据存到redis里面# 分析: pymysql.json.redis# 1.连接数据库,查到数据库里面所有的数据,游标类型要用pymysql.curs ...