hdu1102
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的更多相关文章
- HDU1102(最小生成树Kruskal)
开学第三周.........真快尼 没有计划的生活真的会误入歧途anytime 表示不开心不开心不开心 每天都觉得自己的生活很忙 又觉得想做的事又没有完成 这学期本来计划重点好好学算法,打码码,臭臭美 ...
- HDU1102 Constructing Roads —— 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题解: 纯最小生成树,只是有些边已经确定了要加入生成树中,特殊处理一下这些边就可以了. krus ...
- POJ 2485 Highways && HDU1102(20/200)
题目链接:Highways 没看题,看了输入输出.就有种似曾相识的感觉,果然和HDU1102 题相似度99%,可是也遇到一坑 cin输入居然TLE,cin的缓存不至于这么狠吧,题目非常水.矩阵已经告诉 ...
- hdu1102 Constructing Roads (简单最小生成树Prim算法)
Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...
- 【HDU1102】Constructing Roads(MST基础题)
最小生成树水题.prim一次AC #include <iostream> #include <cstring> #include <cstdlib> #includ ...
- POJ2421 & HDU1102 Constructing Roads(最小生成树)
嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree? orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告 ...
- HDU1102(最小生成树Kruskal算法)
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu1102(最小生成树水题)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...
- HDU1102 最小生成树prim算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:给出任意两个城市之间建一条路的时间,给出哪些城市之间已经建好,问最少还要多少时间使所有的城 ...
随机推荐
- 1.servlet hello实例---HelloServlet
1.用url调用servlet 建 web project “HelloServlet”,在src下建包com.amaker.servlet,在包下建HelloServlet.java. packag ...
- jsp探针
在网上找到一些jsp探针,收藏下. JSP探针1.jsp <%@ page contentType="text/html;charset=gb2312" %> < ...
- Linux 网络工具之fping
系统中有个最最常用命令工具--ping,而fping 是ping 工具的加强版本.安装过程很简单: [root@mail /]# wget --limit-rate 500k http://www.f ...
- rhel_7.x 安装mysql
http://database.51cto.com/art/201310/413006.htm MariaDB和MySQL --mysql-5.7.12-1.el7.x86_64.rpm-bundle ...
- 节点属性(DOM对象)
节点属性 在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeType ...
- Z-BlogPHP 安装出现 (8) Undefined offset: 6 解决方法
有些cp面板的空间会在每个网页头部和页脚增加两个调用的文件,导致zblogPHP安装出错:(8) Undefined offset: 6 主要国外的主机中PHP配置文件两个选项auto_prepend ...
- pandas库学习笔记(二)DataFrame入门学习
Pandas基本介绍——DataFrame入门学习 前篇文章中,小生初步介绍pandas库中的Series结构的创建与运算,今天小生继续“死磕自己”为大家介绍pandas库的另一种最为常见的数据结构D ...
- mysql基础三(视图、触发器、函数、存储过程、事务、防注入)
一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. 1.创建视图 -格式:CREATE ...
- Redis 四:存储类型之列表类型
.lpush num 依次从左边推入0 - .rpush num 依次从右边推入0 - .lrnage num - 显示num列表中所有的数据 结果: .lpop num 从左边删除并弹出一个元素 . ...
- 查看MYSQL数据库中所有用户及拥有权限
查看MYSQL数据库中所有用户 mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM m ...