题目大意:输入一个整数n,表示村庄的数目。在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离。(下面会结合样例说明)。接着,输入一个整数q,表示已经有q条路修好。

在接下来的q行中,会给出修好的路的起始村庄和结束村庄。。

输入样例说明如下:

解题思路:最小生成树(kruscal算法)

1)以前的题会直接给村庄编号以及村庄距离。而这道题,这是给出村庄的距离矩阵。村庄的编号信息蕴含在

矩阵中。这时候的读取方法为:

for(i = 1 ; i <= n ; ++i){
for(j = i + 1 ; j <= n ; ++j){
e[count].begin = i;
e[count].end = j;
e[count].weight = map[i][j];
count++;
}
}

2)点的起始编号与变得起始编号没有必然的联系。即点可以从1开始计数,而边则从0开始计数。

for( i = 1 ; i < maxn ; ++i){
father[i] = i;
} for( i = 0 ; i < count ; ++i){
int fx = find(e[i].begin);
int fy = find(e[i].end); if(fx != fy){
father[fx] = fy;
sum += e[i].weight;
}
}

3)已修的路,令weight为0即可。

代码如下:

/*
* 1102_1.cpp
*
* Created on: 2013年8月26日
* Author: Administrator
*/ #include <iostream> using namespace std; struct edge{
int begin;
int end;
int weight;
}; const int maxn = 6000;
int father[maxn];
edge e[maxn*maxn]; int find(int x){
if( x == father[x]){
return x;
} father[x] = find(father[x]);
return father[x];
} int kruscal(int count){
int i;
int sum = 0; for( i = 1 ; i < maxn ; ++i){
father[i] = i;
} for( i = 0 ; i < count ; ++i){
int fx = find(e[i].begin);
int fy = find(e[i].end); if(fx != fy){
father[fx] = fy;
sum += e[i].weight;
}
} return sum;
} bool compare(const edge& a , const edge& b){
return a.weight < b.weight;
} int main(){
int n;
while(scanf("%d",&n)!=EOF){
int i,j;
int map[n+1][n+1];
for( i = 1 ; i <= n ; ++i){
for( j = 1 ; j <= n ; ++j){
scanf("%d",&map[i][j]);
}
}
int q;
scanf("%d",&q);
for( i = 1 ; i <= q ; ++i){
int a ,b;
scanf("%d%d",&a,&b);
map[a][b] = 0;
}
int count = 0;
for(i = 1 ; i <= n ; ++i){
for(j = i + 1 ; j <= n ; ++j){
e[count].begin = i;
e[count].end = j;
e[count].weight = map[i][j];
count++;
}
} sort(e, e + count , compare); int sum = kruscal(count); printf("%d\n",sum); }
}

(step6.1.4)hdu 1102(Constructing Roads——最小生成树)的更多相关文章

  1. HDU 1102 Constructing Roads (最小生成树)

    最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...

  3. HDU 1102 Constructing Roads(最小生成树,基础题)

    注意标号要减一才为下标,还有已建设的路长可置为0 题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<str ...

  4. HDU 1102 Constructing Roads, Prim+优先队列

    题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...

  5. HDU 1102(Constructing Roads)(最小生成树之prim算法)

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

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

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

  7. hdu 1102 Constructing Roads (Prim算法)

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

  8. hdu 1102 Constructing Roads Kruscal

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...

  9. HDU 1102 Constructing Roads

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

随机推荐

  1. ubuntu 常用软件

    terminator:任意分割控制台 Sublime Text:文本编辑器,也是轻量级的IDE Wireshark:抓包工具 Okular:PDF等文档编辑工具 yEd:流程图等制图软件 Shutte ...

  2. SQL注入(一)普通型注入

    既然说了从头开始,先从注入开始吧,先来温习一下之前会的一些注入. PHP注入 0x01: 判断是否存在注入: '   报错 ' and 1=1   正确 ' and 1=2   错误 0x01: or ...

  3. [置顶] gis海量资源网盘提供VIP账号无广告高速下载 (更新更多资源)

    资源网盘下载地址:http://laoheitan.bego.cc/ 城通网盘 vip帐号共享 省去 烦人的 广告  多任务同时下载 独乐乐 不如众乐乐 好人 勿改密码. 获取到 vip下载连接后 请 ...

  4. 菜鸟nginx源代码剖析数据结构篇(一)动态数组ngx_array_t

    菜鸟nginx源代码剖析数据结构篇(一)动态数组ngx_array_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csd ...

  5. PHP移动互联网开发笔记(5)——文件的上传下载

    原文地址:http://www.php100.com/html/php/rumen/2014/0326/6706.html 一.文件的上传 1.client设置: (1).在 标签中将enctype和 ...

  6. MySQL filesort优化案例一则

    今天遇到一个filesort优化的案例,感觉不错,分享出来. MySQL中filesort是什么意思?官方手册定义: MySQL must do an extra pass to find out h ...

  7. bonjour

    首先bonjour并非必须的,可是它的确非常方便,假设没有它我们须要指定ip地址进行局域网的传输,有了它就能够依据服务的详细的名称来选择服务,能够这样来理解bonjour就相当于hostname,我们 ...

  8. Deep Learning深入研究整理学习笔记五

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  9. 【源代码】LinkedHashMap源代码剖析

    注:下面源代码基于jdk1.7.0_11 之前的两篇文章通过源代码分析了两种常见的Map集合,HashMap和Hashtable.本文将继续介绍还有一种Map集合--LinkedHashMap. 顾名 ...

  10. vc 在edit控件中动态插入数据滚动显示

    内存从网上论坛摘抄整理 思路:给控件设置多行属性,设置垂直滚动条,Auto Vscroll设置为true,放入文本后把插入点设置到末尾 pEdit->LineScroll(pEdit->G ...