题目大意:输入一个整数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. 网页调试技巧:抓取马上跳转的页面POST信息或者页面内容

    http://www.qs5.org/Post/625.html 网页调试技巧:抓取马上跳转的页面POST信息或者页面内容 2016/02/02 | 心得分享 | 0 Replies 有时候调试网页或 ...

  2. 深入浅出Hive企业级架构优化、Hive Sql优化、压缩和分布式缓存(企业Hadoop应用核心产品)

    一.本课程是怎么样的一门课程(全面介绍)    1.1.课程的背景       作为企业Hadoop应用的核心产品,Hive承载着FaceBook.淘宝等大佬 95%以上的离线统计,很多企业里的离线统 ...

  3. span设置固定宽度

    <span> 标签是被用来组合文档中的行内元素.相信对一般的网页设计师来讲是非常熟悉的朋友了,使用相当频繁,但我们往往很少对SPAN设定样式,一般也没什么必要,大多数都留给DIV老朋友了. ...

  4. Mysql 执行计划分析

    zjdev 正常访问: mysql> explain SELECT temp.* , -> (SELECT COUNT(sn) FROM AssignClientManager WHERE ...

  5. 2014 Multi-University Training Contest 1 — D. Task

    题目链接:pid=4864">http://acm.hdu.edu.cn/showproblem.php?pid=4864 题目大意: 有N个机器.M个任务. 当中每一个机器有xi,y ...

  6. ASIHTTPRequest开源类项目导入问题及解决方法

    在静态库project中加入ASIHTTPRequest导出lib.a.放到project里编译出一下错: Undefined symbols for architecture armv7: &quo ...

  7. Android API中被忽略的几个函数接口

    1. MotionEvent的几个函数 下面的方法都支持多点触摸,即可以对单个触摸点调用下面的方法 1.1 getPressure() 这个api 可以获取到手指触摸屏幕时候的压力,但是需要硬件和驱动 ...

  8. 积累的VC编程小技巧之框架窗口及其他

    1.修改主窗口风格 AppWizard生成的应用程序框架的主窗口具有缺省的窗口风格,比如在窗口标题条中自动添加文档名.窗口是叠加型的.可改变窗口大小等.要修改窗口的缺省风格,需要重载CWnd::Pre ...

  9. Sql Server中COUNT(字段名)跟COUNT(*)的特殊不同点

    今天有个需求,有2张表: 1.一个“搜索记录”表search,一个“搜索后下载记录”表down 2.映射关系:每一个下载记录对应一条搜索记录,                           第个 ...

  10. Test oracle db iops

    Today, i need to test one database's iops and do something for oracle db's io test. How to test the ...