hdoj 1102 Constructing Roads
题目: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的更多相关文章
- Hdoj 1102.Constructing Roads 题解
Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...
- (MST) HDOJ 1102 Constructing Roads
怎么说呢 这题就是个模板题 但是 hud你妹夫啊说好的只有一组数据呢??? 嗯??? wa到家都不认识了好吗 #include <cstdio> #include <cstring& ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- HDU 1102 Constructing Roads, Prim+优先队列
题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...
- HDU 1102(Constructing Roads)(最小生成树之prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...
- hdu 1102 Constructing Roads (Prim算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- hdu 1102 Constructing Roads (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- hdu 1102 Constructing Roads Kruscal
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...
- HDU 1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- WP开发笔记——日期时间DateTime.Now函数
//2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...
- Fedora 20 创建桌面快捷方式
创建desktop文件 sudo touch /usr/share/applications/sublime.desktop 添加内容 [Desktop Entry] Encoding=UTF-8 N ...
- grep恢复误删除文件内容(转)
在 Linux 上如果事先没有用别名(alias)修改默认的 rm 功能,rm 后文件就会丢失,幸运的是,在一般的删除文件操作中,Linux 并不会立即清空存储该文件的 block 内容,而只会释放该 ...
- c# 分页控件
因为最近做的培训项目需要用到很多分页功能,自己写了一个分页方法,想找个时间重构,看到一篇博客,转载过来,分享学习. 原文链接:http://www.cnblogs.com/rr163/p/395593 ...
- Golang container/ring闭环数据结构的使用方法
//引入包 import "container/ring" //创建闭环,这里创建10个元素的闭环 r := ring.New(10) //给闭环中的元素附值 for i := 1 ...
- 解决Handler与Activity同步冲突
这个问题可以由Handler的一个子类HandlerThread来解决. 程序参考自Mars老师的Android课程第一季第十五集. 代码以及注释有所改动,如下: package com.handle ...
- openerp学习笔记 视图更新时删除已存在的菜单或其他对象
删除菜单示例: <delete id="base.menu_module_updates" model="ir.ui.menu"/><dele ...
- selenium+python 浏览器标签页跳转 switch_to_window
浏览器页面跳转方法记录: from selenium import webdriver import time browser = webdriver.Chrome() first_url='http ...
- 无法打开物理文件xxx.mdf 操作系统错误 5:“5(拒绝访问。)” (Microsoft SQL Server,错误: 5120) 的解决方法
问题描述:在附加数据库到sql server时,附加不上,出现如下图所示的错误 解决方法:找到xxx.mdf和xxx_log.ldf文件, 点击“右键”->“属性”->"安全&q ...
- ubuntu 下dbus的环境搭建和使用
从https://launchpad.net/ubuntu/+source/dbus/1.10.6-1ubuntu2下载需要的dbus包,然后解压,./configure make && ...