开学第三周。。。。。。。。。真快尼

没有计划的生活真的会误入歧途anytime

表示不开心不开心不开心 每天都觉得自己的生活很忙 又觉得想做的事又没有完成 这学期本来计划重点好好学算法,打码码,臭臭美,做点兼职尼 坚持早起啊,一定一定!!!!坚持到成功为止!要不然之前努力就白费了 我想抽空看电影。。。学做PPt。。。口语。。。不能停。。。。。6级。。。。。觉得时间不过用尼  发现白天的生活就是死人一般啊。。。这些恼人的课ssssss和死板的teachersssssssss 抓 狂 我自我感爆棚了!!!!无心相处 ,,,,, 根本做不上自己的事啊  ~~~~ 社会水太深  宝宝心好累   按时睡觉按时起  练口语  小爷啥时候做我的6题尼 课上的时间还是得好好地吧 白天的空课 留给他们6科 晚上代码算法 java咋办尼 放在这个时间里吧 简单的熟悉还没有做到尼 熟练了就行 关于兼职 小爷心里苦啊  真的有些糟心尼 歇几天 既然是大人了 早晚都要学着平衡这些东西才是尼本月减肥20天!!!! 最重要的是代码算法 第二阶段啊!!!!不能舍本逐末 !!!!生活不是是眼前的苟且 还有诗和远方   调整好自己 充实精致的自己

 

hdu1102 Constructing Roads(kruskal)

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6701    Accepted Submission(s): 2475

Problem Description
There are N villages, which are numbered from
1 to N, and you should build some roads such that every two villages
can connect to each other. We say two village A and B are connected, if
and only if there is a road between A and B, or there exists a village C
such that there is a road between A and C, and C and B are connected.

We
know that there are already some roads between some villages and your
job is the build some roads such that all the villages are connect and
the length of all the roads built is minimum.

 
Input
The first line is an integer N (3 <= N
<= 100), which is the number of villages. Then come N lines, the i-th
of which contains N integers, and the j-th of these N integers is the
distance (the distance should be an integer within [1, 1000]) between
village i and village j.

Then there is an integer Q (0 <= Q
<= N * (N + 1) / 2). Then come Q lines, each line contains two
integers a and b (1 <= a < b <= N), which means the road
between village a and village b has been built.

 
Output
You should output a line contains an integer,
which is the length of all the roads to be built such that all the
villages are connected, and this value is minimum.
 
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
 
Sample Output
179
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int fa[];
int n,m;
int cc;
struct edge
{
int u,v,w;
void set(int x,int y,int s)
{
u=x;
v=y;
w=s;
} }e[];
int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
}
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
void kruskal()
{
int ans=;cc=n;
sort(e,e+n*n,cmp);
for(int i=; i<n*n; i++)
{
int x=find(e[i].u);
int y=find(e[i].v);
if(x!=y)
{
fa[x]=y;
ans+=e[i].w;
cc--;
}
}
if(cc==)
printf("%d\n",ans);
} void init()
{
for(int i=; i<n; i++)
fa[i]=i; }
int main()
{
while(cin>>n)
{
init();
int cnt=;int d[][];
for(int i=; i<n; i++)
for(int j=; j<n; j++)
scanf("%d",&d[i][j]);
int Q;
cin>>Q;
int a,b;
for(int i=; i<Q; i++)
{
scanf("%d%d",&a,&b);
d[a-][b-]=;
}
for(int i=; i<n; i++)
for(int j=; j<n; j++)
{ e[cnt].set(i,j,d[i][j]); cnt++;
}
kruskal(); }
return ;
}

快赶上生哪吒了的进度。。。。

HDU1102(最小生成树Kruskal)的更多相关文章

  1. HDU1102(最小生成树Kruskal算法)

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

  2. 模板——最小生成树kruskal算法+并查集数据结构

    并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...

  3. 最小生成树——Kruskal与Prim算法

    最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个 ...

  4. 【转】最小生成树——Kruskal算法

    [转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...

  5. 最小生成树 kruskal算法 codevs 1638 修复公路

    1638 修复公路  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description A地区在地震过后,连接所有村庄的公 ...

  6. 最小生成树——kruskal算法

    kruskal和prim都是解决最小生成树问题,都是选取最小边,但kruskal是通过对所有边按从小到大的顺序排过一次序之后,配合并查集实现的.我们取出一条边,判断如果它的始点和终点属于同一棵树,那么 ...

  7. 贪心算法-最小生成树Kruskal算法和Prim算法

    Kruskal算法: 不断地选择未被选中的边中权重最轻且不会形成环的一条. 简单的理解: 不停地循环,每一次都寻找两个顶点,这两个顶点不在同一个真子集里,且边上的权值最小. 把找到的这两个顶点联合起来 ...

  8. 最小生成树---Kruskal/Prime算法

    1.Kruskal算法 图的存贮采用边集数组或邻接矩阵,权值相等的边在数组中排列次序可任意,边较多的不很实用,浪费时间,适合稀疏图.      方法:将图中边按其权值由小到大的次序顺序选取,若选边后不 ...

  9. 【BZOJ-2177】曼哈顿最小生成树 Kruskal + 树状数组

    2177: 曼哈顿最小生成树 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 190  Solved: 77[Submit][Status][Discu ...

随机推荐

  1. Linux Basic --- The First Character in The File Properity

    -rw-r--r-- [d]: content [-]: file [l]: link file [b]: interface device for storage in a device file ...

  2. jQuery基础(1) -- jQuery 语法

    通过 jQuery,您可以选取(查询,query) HTML 元素,并对它们执行"操作"(actions).jQuery 语法jQuery 语法是通过选取 HTML 元素,并对选取 ...

  3. JS-Math对象

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>M ...

  4. EmguCV 一些基本操作

    http://www.cnblogs.com/alsofly/p/3524866.html?utm_source=tuicool&utm_medium=referral 一.先是在程序中图像的 ...

  5. StringUtils 的常用方法

    StringUtils 方法的操作对象是 Java.lang.String 类型的对象,是 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String 为 ...

  6. uC/OS-II实现TEST.MAK块

    ################################################################################                     ...

  7. 添加IP安全策略 远离系统Ping漏洞的威胁

    懂得网络的人对于Ping这个最基本的网络命令一定很熟悉,它是一个非常好用的TCP/IP工具.它可以向你提供的地址发送一个小的数据包,然后侦听这台机器是否有“回答”.你可以使用机器的 Internet ...

  8. TCP/UDP端口列表

    http://zh.wikipedia.org/wiki/TCP/UDP%E7%AB%AF%E5%8F%A3%E5%88%97%E8%A1%A8 TCP/UDP端口列表     本条目可通过翻译外语维 ...

  9. Eclipse中修改Web项目的URL访问路径

    背景 访问路径,也就是指在浏览器中访问该web系统时的根路径,比如http://localhost:8080/xxxx/index.jsp  这里的xxxx,也就是request.getContext ...

  10. QT 初阶 1.3 节 控件的几何排列

    #include "mainwindow.h" #include <QApplication> #include <QHBoxLayout> #includ ...