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 ...
随机推荐
- some windowsphone templates
http://inspirationfeed.com/freebies/20-free-windows-phone-7-mockup-and-wireframing-resources/
- ARM公布“物联网”嵌入式mbed OS系统软件平台
继ARM公司发布了为嵌入式微控制器设计的Cortex-M7架构处理器,ARM又公布了专为廉价低功耗“物联网”设计的新版软件及系统平台,以加速物联网设备的发展及部署.该软件为基于ARM现有Cortex- ...
- nginx配置多个网址
实战Nginx与PHP(FastCGI)的安装.配置与优化:http://ixdba.blog.51cto.com/2895551/806622 Nginx配置文件详细说明:http://www.cn ...
- nss_12 上传文件后返回jsonresult结果,IE中出现文件下载框
因为控制器返回的是JsonResult, 但是在IE8中一直返回文件下载的对话框. 转到谷歌浏览器倒没有问题. 网上找的方法, 要么是跟到一个新的成功页面, 要么是直接返回html, 觉得应该有更好的 ...
- Visual Studio 2012 使用免费的Team Foundation Service
VS2012提供了在线的TFS服务,免费支持五人小团队,收费情况尚未确定,下面本文演示如何申请和连接在线TFS 服务器. 一.申请TFS服务 首先,打开VS2012,看看是否有团队资源管理器,如果没有 ...
- Hibernate中的对象状态,及自动更新原因
Hibernate的对象有三种状态,分别为:瞬时状态 (Transient). 持久化状态(Persistent).游离状态(Detached).对它的深入理解,才能更好的理解hibernate的运行 ...
- C# 解析XML格式的字符串
public CreateOrderReturnResult GetCreateOrderReturnApi() { var result = new CreateOrderReturnResult( ...
- Hadoop介绍及最新稳定版Hadoop 2.4.1下载地址及单节点安装
Hadoop介绍 Hadoop是一个能对大量数据进行分布式处理的软件框架.其基本的组成包括hdfs分布式文件系统和可以运行在hdfs文件系统上的MapReduce编程模型,以及基于hdfs和MapR ...
- (转)汉字转拼音HanziToPinyin
本文转载于:http://blog.csdn.net/zhangphil/article/details/47164665 Android系统本身自带有有将汉字转化为英文拼音的类和方法.具体的类就是H ...
- 简单的下拉刷新以及优化--SwipeRefreshLayout
代码工程简要说明:以一个SwipeRefreshLayout包裹ListView,SwipeRefreshLayout接管ListView的下拉事件,若ListView被用户触发下拉动作后,Swipe ...