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 题意:给出任意两个城市之间建一条路的时间,给出哪些城市之间已经建好,问最少还要多少时间使所有的城 ...
随机推荐
- Linux上iptables防火墙的基本应用
1.安装iptables防火墙 yum install iptables -y 2. 清除已有的iptables规则 iptables -F iptables -X iptables -Z 3.显示i ...
- 关于ionic的跨域问题
例如你的api原地址请求是 http://10.100.100.100:8080/service/, 1.那么你应该在项目内api请求改成 /service/, 注意红色部分是ionic serve ...
- JS去除数组中重复值的四种方法
JS去除数组中重复值的四种方法 1 /// <summary> o[this[i]] = ""; } } newArr.p ...
- [转]Linux中find常见用法示例
Linux中find常见用法示例[转]·find path -option [ -print ] [ -exec -ok command ] {} \;find命令的参 ...
- MvvmCross for WPF File Plugin
本文以MvvmCross为框架基础 最近用了File Plugin插件,一开始也是没用明白,写一下记录下来,也方便需要的人吧 首先这个File Plugin需要先在UI项目里创建一个Bootstrap ...
- 【转】Microsoft® SQL Server® 2012 Performance Dashboard Reports
http://www.cnblogs.com/shanyou/archive/2013/02/12/2910232.html SQL Server Performance Dashboard Repo ...
- UserLogin
DAL: IUserDAL namespace Dal { /// <summary> /// This interface is defined for user functions. ...
- c语言调试开关
上一篇转载的没看懂,参考别人的代码,自己又琢磨了一个调试技巧,挺好用,姑且就叫调试开关吧,欢迎指正!!! /*功能:调试开关 *描述:if条件成立,则打印调试信息,否则不打印() * */ #incl ...
- 小甲鱼汇编语言006第二章 寄存器(CPU工作原理)01
http://baidu.ku6.com/watch/09215216064281951074.html?page=videoMultiNeed
- 命令行插入含有中文的sql文件,报错ERROR 1366 (HY000): Incorrect stringvalue:
--以下是插入语句: insert into sms_inbox values('123456','123456', 'cd', sysdate(), '今天天 气很好', 1, sysdate(), ...