注意标号要减一才为下标,还有已建设的路长可置为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. Qt学习总结-ui篇

    控件设置透明度: QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this); effect->setOpacity(0. ...

  2. Netfilter深度解剖

         在网络上发现这个Netfilter写的很好的系列文章,为了便于后期寻找,特此标注下博客地址,感谢大大神提供.     ---------------------------分割线开始---- ...

  3. MVC校验

    首先Model里面需要写好校验标签, 我的数据库中有个tblUserInfo表,其中有Id,UserName,,Age三个列,Id自动增长 Model添加UserInfo Class,在UserNam ...

  4. rest介绍

    REST介绍 描述 rest即表述性状态传递(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软 ...

  5. Tomcat启动超时

    当启动tomcat时候出现 Server Tomcat v8.0 Server at localhost was unable to start within 45 seconds. If the s ...

  6. HTML:form表单总结,input,select,option,textarea,label

    <form>标签是块级元素. form标签的标准属性有id,class,style,title,lang,xml:lang. 表单能够包含input元素(包含button,checkbox ...

  7. aspx返回json数据

    JQuery.getJSON 从aspx页面返回JSON数据 . -- ::| 分类: asp.net |举报|字号 订阅 . 发送请求的WebForm1.aspx <%@ Page Langu ...

  8. python autopy

    今天学习了python autopy 主要包括alert,color,mouse,key,bitmap,screen等的操作 文档在http://www.autopy.org/documentatio ...

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

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

  10. Visual Studio

    1.必备神器http://www.cnblogs.com/stoneniqiu/p/3488546.html 2.常用快捷键http://www.cnblogs.com/TankXiao/p/3468 ...