注意标号要减一才为下标,还有已建设的路长可置为0

#define  _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include<string.h>
#include <malloc.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
using namespace std; #define M 110
#define inf 999999999
int mat[M][M]; int prim(int n,int sta)
{
int mark[M],dis[M];
int i,sum=,j,flag;
for(i=;i<n;i++)
{
dis[i]=mat[sta][i];
mark[i]=;
}
mark[sta]=;
for(i=;i<n;i++)
{
int minn=inf;
for(j=;j<n;j++)
{
if(dis[j]<minn&&mark[j]==)
minn=dis[j],flag=j;
}
mark[flag]=;
sum+=dis[flag];
for(j=;j<n;j++)
{
if(dis[j]>mat[flag][j])
dis[j]=mat[flag][j];
}
}
return sum;
} int main()
{
int n,i,j,a,b,m;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<n;i++)
{
for(j=;j<n;j++)
{
scanf("%d",&mat[i][j]); }
}
scanf("%d",&m);
for(i=;i<m;i++)
{
scanf("%d%d",&a,&b);//原来还是这里的问题,注意下标和标号还是有区别的,要减一
mat[a-][b-]=mat[b-][a-]=;//如果两村之间的路已经连接好,则他们之间的路程设为0
}
printf("%d\n",prim(n,));
}
return ;
}

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. (step6.1.4)hdu 1102(Constructing Roads——最小生成树)

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

  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. Java中注解Annotation的定义、使用、解析

    此例子,用于说明如何在Java中对“注解 Annotation”的定义.使用和解析的操作.注解一般用于自定义开发框架中,至于为什么使用,此处不作过多说明,这里只说明如何使用,以作备记.下面例子已测试, ...

  2. debian终端菱形乱码修复

    最简安装debian的时候由于没有中文字库,若选择看中文环境会出现菱形乱码.先把zh.utf8换为us.utf8看着好顺眼些.按空格键取消已选的zh.utf8选项按空格键选择us.utf8选项ok

  3. 使用JDBC向数据库中插入一条数据

    原谅我是初学者,这个方法写的很烂,以后不会改进,谢谢 /** * 通过JDBC向数据库中插入一条数据 1.Statement 用于执行SQL语句的对象 1.1 通过Connection 的 * cre ...

  4. Linux 下安装服务器安全狗

    1.网上下载服务器安全狗的软件包    32位和64位 wget http://www.safedog.cn/server_safedog_linux.html/safedog_linux32.tar ...

  5. ZK framework on Java

    Quick start: https://www.zkoss.org/documentation Live demo: https://www.zkoss.org/zkdemo/file_handli ...

  6. WordPress 主题开发 - (五)WordPress 主题模板及目录结构 待翻译

    While the most minimal of WordPress Themes really only need an index.php template and a style.css fi ...

  7. C# CacheHepler Class

    internal class CacheHelper { /// <summary> /// Insert value into the cache using /// appropria ...

  8. vs2008中使用Newtonsoft.Json

    异常:找不到方法:“Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()” 在使用Newtonsof ...

  9. selenium-webdriver用例批量运行和测试套件使用 ------之我见

    用例批量运行和测试套件使用 ------之我见 学习selenium-webdriver已经一段时间了,最近学习到,测试用例的批量执行,和测试套件的使用,有点自己的理解,不晓得对不对,希望大家指正! ...

  10. 【转】 申请对齐某种结构体大小的buffer

    在大多数情况下,编译器和C库透明地帮你处理对齐问题.POSIX 标明了通过malloc( ), calloc( ), 和 realloc( ) 返回的地址对于任何的C类型来说都是对齐的.在Linux中 ...