题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102

分析:看到这题给出的都是矩阵形式,就知道了可以用Prim算法求MST。

#include <iostream>

using namespace std;
#define typec int
#define V 101
const typec inf=0x3f3f3f3f;
int vis[V];
typec lowc[V],cost[V][V];
/*=================================================*\
| Prim求MST
| INIT: cost[][]耗费矩阵(inf为无穷大);
| CALL: prim(cost, n); 返回-1代表原图不连通;
\*==================================================*/
typec prim(int n)
{
int i,j,p;
typec minc,res=;
memset(vis,,sizeof(vis));
vis[]=;
for(i=; i<n; i++) lowc[i]=cost[][i];
for(i=; i<n; i++)
{
minc=inf;
p=-;
for(j=; j<n; j++)
{
if(==vis[j]&&minc>lowc[j])
{
minc=lowc[j];
p=j;
}
}
if(inf==minc) return -;
res+=minc;
vis[p]=;
for(j=; j<n; j++)
if(==vis[j]&&lowc[j]>cost[p][j])lowc[j]=cost[p][j];
}
return res;
} int main()
{
// freopen("input.txt","r",stdin);
int n,m,i,j,u,v;
while(cin>>n&&n!=)
{
memset(cost,inf,sizeof(cost));
for(i=; i<n; i++)
for(j=; j<n; j++)
cin>>cost[i][j];
cin>>m;
for(i=; i<m; i++)
{
cin>>u>>v;
cost[u-][v-]=cost[v-][u-]=;
}
cout << prim(n) << endl;
}
return ;
}

hdoj 1102 Constructing Roads的更多相关文章

  1. Hdoj 1102.Constructing Roads 题解

    Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...

  2. (MST) HDOJ 1102 Constructing Roads

    怎么说呢 这题就是个模板题 但是 hud你妹夫啊说好的只有一组数据呢??? 嗯??? wa到家都不认识了好吗 #include <cstdio> #include <cstring& ...

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

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

  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 (Prim算法)

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

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

    题目链接: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. C++ Sets

    集合(Set)是一种包含已排序对象的关联容器 begin() 返回指向第一个元素的迭代器 clear() 清除所有元素 count() 返回某个值元素的个数 empty() 如果集合为空,返回true ...

  2. iOS人机界面指南(翻译)

    本文源自于苹果开发者网站的文章iOS Human Interface Guidelines,内容比较多,此处仅仅是部分笔记.

  3. Stanford parser:入门使用

    一.stanford parser是什么? stanford parser是stanford nlp小组提供的一系列工具之一,能够用来完成语法分析任务.支持英文.中文.德文.法文.阿拉伯文等多种语言. ...

  4. Ubuntu 在未知root密码的情况下修改root密码

    一, 开机按 F12 (或长按Shift), 进入GRUB界面. 二, 在 recovery mode 按e Ubuntu, Linux 3.5.0-17-generic (恢复模式) (或recov ...

  5. jquery的$(document).ready()和onload的加载顺序

    最近在改一个嵌入在frame中的页面的时候,使用了jquery做效果,而页面本身也绑定了onload事件.改完后,Firefox下测试正常流畅,IE下就要等个十几秒jquery的效果才出现,黄花菜都凉 ...

  6. Oracle bbed使用说明2---常用命令

    一.BBED常用命令说明 先看帮助的说明 BBED> help all SET DBA [ dba | file#, block# ] SET FILENAME 'filename' SET F ...

  7. sqlalchemy - day1

    一.Create engine Database url规则: dialect+driver://username:password@host:port/database echo: True表示cm ...

  8. XAML 概述二

    通过上一节我们已经对XAML有了一定的了解,这一节我们来系统的学习一下XAML. 一. 简单属性与类型转换器,属性元素: 我们已经知道 XAML是一种声明性的语言,并且XAML解析器会为每个标签创建一 ...

  9. 扒一扒各大电商网站的m站都用的什么前端技术输入日志标题

    凡客首页使用Swiper和zepto,没有使用jquery , 静态首页+js交互,  资源加载使用 lazyLoad X-AspNet-Version: 4.0.30319 X-AspNetMvc- ...

  10. UIActivityIndicatorView的使用

    class ViewController: UIViewController,UIActionSheetDelegate{ @IBOutlet weak var label1: UILabel! @I ...