题目大意:输入一个整数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. html5 canvas 实现一个简单的叮当猫头部

    原文:html5 canvas 实现一个简单的叮当猫头部 html5的canvas是很强大的,今天也是温习了一下之前的基础知识,然后学着做了一个简单的小案例.虽然在这一块几乎空白,但还是乐于尝试... ...

  2. 基于visual Studio2013解决面试题之0507字符串转整数

     题目

  3. Visual Studio 必备神器---转

    会使用工具是人类文明的一大进步,今天敏捷大行其道,好的工具可以大大的提高生产力,这里说的工具都是VS平台上的扩展工具,一些机械的部分可以交给工具去处理,自己多关注其他部分.下面分享下我觉得不错的工具, ...

  4. 微软HR泄露的asp.net面试题

    1.面向对象的思想主要包括什么? 2.什么是ASP.net中的用户控件? 3.什么叫应用程序域?什么是受管制的代码?什么是强类型系统?什么是装箱和拆箱?什么是重载? 4.列举一下你所了解的XML技术及 ...

  5. Eclipse使用总结

    Eclipse使用总结 1.Eclipse中出现无法找到Maven包     症状:出现org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER, 且出现无法找 ...

  6. Swift - 数字格式化转成字符串(保留两位小数)

    1,下面是一个浮点类型的数字转成String字符串的例子 1 2 var f = 123.32342342 var s:String = "\(f)" //123.32342342 ...

  7. Swift - 动画效果的实现方法总结(附样例)

    在iOS中,实现动画有两种方法.一个是统一的animateWithDuration,另一个是组合出现的beginAnimations和commitAnimations.这三个方法都是类方法. 一,使用 ...

  8. Spinner的用法实现

    界面上只有一个textview和一个spinner,实现下拉列表框. spinner.xml: <?xml version="1.0" encoding="utf- ...

  9. perl 异步超时 打印错误

    #!/usr/bin/perl use AnyEvent; use AnyEvent::HTTP; my $cv = AnyEvent->condvar; sub doit{ my $url = ...

  10. ThinkPhp学习10

    原文:ThinkPhp学习10 查询操作 Action模块 User下的search public function search(){ //判断username是否已经传入,且不为空 if(isse ...