http://acm.hdu.edu.cn/showproblem.php?pid=1102

最小生成树(模板题)

3

0 990 692

990 0 179

692 179 0

1

1 2

一共3个村子,下面是第i行j列 i村子和j村子之间建路需要的距离

下面是一个k

代表有k条路已经修好了,1村子和2村子之间以修路

 #include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
const int N=;
struct stu{
int u;
int v;
int w;
}p[N];
int q[][];
int father[N];
int n,m;
int cmp(const void *a,const void *b)
{
return (*(struct stu*)a).w > (*(struct stu*)b).w ?:-;
}
int find(int x)
{
if(father[x]!=x)
father[x]=find(father[x]);
return father[x];
}
int make(int a,int b)
{
int h=;
int f1=find(a);
int f2=find(b);
if(f1>f2)
{
father[f1]=f2;
h=;
}
else if(f1<f2)
{
father[f2]=f1;
h=;
}
return h;
}
int kruskal()
{
int cnt=;
int s=;
for(int i=;i<m;i++)
{
if(make(p[i].u,p[i].v))
{
cnt++;
s+=p[i].w;
}
if(cnt==n-)
return s;
}
return s;
} int main()
{
//freopen("in.txt","r",stdin);
int k;
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++)
father[i]=i;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&q[i][j]);
}
}
scanf("%d",&k);
int a,b;
while(k--)
{
scanf("%d%d",&a,&b);
q[a][b]=;//已经建路,清0
}
m=;
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
p[m].u=i;
p[m].v=j;
p[m++].w=q[i][j];
}
}
qsort(p,m,sizeof(struct stu),cmp);
// for(int i=0;i<m;i++)
// {
// printf("%d %d %d\n",p[i].u,p[i].v,p[i].w);
// }
printf("%d\n",kruskal()); }
return ;
}

hdu1102的更多相关文章

  1. HDU1102(最小生成树Kruskal)

    开学第三周.........真快尼 没有计划的生活真的会误入歧途anytime 表示不开心不开心不开心 每天都觉得自己的生活很忙 又觉得想做的事又没有完成 这学期本来计划重点好好学算法,打码码,臭臭美 ...

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

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

  3. POJ 2485 Highways &amp;&amp; HDU1102(20/200)

    题目链接:Highways 没看题,看了输入输出.就有种似曾相识的感觉,果然和HDU1102 题相似度99%,可是也遇到一坑 cin输入居然TLE,cin的缓存不至于这么狠吧,题目非常水.矩阵已经告诉 ...

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

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

  5. 【HDU1102】Constructing Roads(MST基础题)

    最小生成树水题.prim一次AC #include <iostream> #include <cstring> #include <cstdlib> #includ ...

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

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

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

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

  8. hdu1102(最小生成树水题)

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...

  9. HDU1102 最小生成树prim算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:给出任意两个城市之间建一条路的时间,给出哪些城市之间已经建好,问最少还要多少时间使所有的城 ...

随机推荐

  1. js设计模式(6)---适配器模式

    0.前言 脖子又开始痛了,难道还没成为码农就开始出现颈椎问题,一直以来举得自己不算那种死宅的人,怎么这么年轻就出现这种问题.哎,不管了,还是先把自己学习的适配器模式写出来,算是一种总结吧. 1.为什么 ...

  2. webBrowser执行js的方法,并返回值,c#后台取值

    private void Form1_Load(object sender, EventArgs e) { webBrowser1.Navigate(Application.StartupPath + ...

  3. 如何判断PHP 是ts还是nts版的

    通过phpinfo(); 查看其中的 Thread Safety 项,这个项目就是查看是否是线程安全,如果是:enabled,一般来说应该是ts版,否则是nts版.

  4. php中echo、print、print_r、printf的返回值

    1.echo 无返回值,是一个语言结构.在输出多个参数时不可以使用小括号; 2.print返回值为1:如:$x = 0; echo print $x."<br/>";/ ...

  5. SQL中char,varchar,nvarchar等的异同

    比较这几个数据类型,总是忘记,可能比较细节的原因.先做个记号,回头完善.

  6. 使用CSS画一个三角形

    <div style="width:0px;height:0px;border-width:40px;border-style:solid;border-color:transpare ...

  7. Keil uVision4 代码编辑器中文字符乱码问题

    MDK-ARM 使用中一直有个很纠结的问题,中文字符支持不好. 比如写代码注释,使用中文删除字符就会只删除一半问题.复制粘贴代码中间有中文就会出现乱码问题. 想过换IAR,新学个IDE也麻烦,上面的问 ...

  8. 拥抱ARM妹纸第二季 之 第一次 点亮太阳

    上次做鱼缸LED灯时还有很多材料正好拿来用.穆等等哥- 俺去找材料. 材料列表     3W LED   x  3     散热片     x  1     恒流IC     x  1     其他零 ...

  9. elipse+pydev+python开发arcgis脚本程序

    环境配置参考:http://www.cnblogs.com/halfacre/archive/2012/07/22/2603848.html 添加arcpy类库.arctoolbox.arcgis-b ...

  10. Seafile V4.1 安装笔记

    yum -y install gcc gcc-c++ make cmake pcre pcre-devel expat expat-devel curl wget mlocate gd gd-deve ...