最小生成树 D - Constructing Roads
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
Input
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Output
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iomanip>
using namespace std;
#define MAXN 104
#define INF 0x3f3f3f3f
bool been[MAXN];
int n,q,g[MAXN][MAXN],lowcost[MAXN];
/*
最小生成树,如果D(a,b)<=ra+rb,那么g[a][b]=0
否则D(a,b)>ra+rb,g[a][b] = ra+rb-D(a,b)
*/
int Prim(int beg)
{
int ans = ;
memset(been,false,sizeof(been));
for(int i=;i<=n;i++)
{
if(i==beg) lowcost[i] = ;
else lowcost[i] = g[beg][i];
}
been[beg] = true;
for(int j=;j<n;j++)
{
int Minc = INF,k=-;
for(int i=;i<=n;i++)
{
if(!been[i]&&lowcost[i]<Minc)
{
Minc = lowcost[i];
k = i;
}
}
if(k==-) return -;
been[k] = true;
ans+=Minc;
for(int i=;i<=n;i++)
{
if(!been[i]&&lowcost[i]>g[k][i])
lowcost[i] = g[k][i];
}
}
return ans;
}
int main()
{
cin>>n;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
cin>>g[i][j];
}
}
cin>>q;
int tx,ty;
for(int i=;i<=q;i++)
{
cin>>tx>>ty;
g[tx][ty] = g[ty][tx] = ;
}
int ans = Prim();
cout<<ans<<endl;
return ;
}
最小生成树 D - Constructing Roads的更多相关文章
- (最小生成树)Constructing Roads -- poj -- 2421
链接: http://poj.org/problem?id=2421 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2113 ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- POJ - 2421 Constructing Roads 【最小生成树Kruscal】
Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...
- hdu Constructing Roads (最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...
- 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 (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- Constructing Roads(1102 最小生成树 prim)
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- deepin 安装 idea
1.su root 2.sudo apt install idea 3.sudo vi /etc/hosts 最后一行添加 0.0.0.0 account.jetbrains.com 4.注册码 N7 ...
- magento后台开发学习笔记(入门实例向)
目的是做一个grid,参考博客http://www.sunzhenghua.com/magento-admin-module-development-part1-grid-forms-tabs-con ...
- Java compiler level does not match the version of the installed Java project facet问题处理
从SVN上下载应用后在Problems面板中提示以下错误信息: Java compiler level does not match the version of the installed Java ...
- 小白的python之路 序
计算机专科毕业,.net开发已有8年有余,中途断断续续,似懂非懂,积累了一些经验知识,但是不求甚解,属于那种一瓶不满半瓶子晃荡,这么一个状态. 主要从事web开发,涉及一些前端jq等,还有接口开发,搜 ...
- iOS 项目中的常见文件
iOS的笔记-项目中的常见文件 新建一个项目之后,有那么多的文件,下面介绍一下主要的几个. 1.文件名 (1)AppDelegate UIApplication的代理,app收到干扰的时候,进行处 ...
- NodeJs学习记录(一)初步学习,杂乱备忘
2016/12/26 星期一 1.在win7下安装了NodeJs 1)进入官网 https://nodejs.org/en/download/,下载对应的安装包,我目前下载的是node-v6.2.0- ...
- WebApi实现IHttpControllerSelector问题
一.让Web API路由配置也支持命名空间参数/// <summary> /// controller /// 选择器 /// </summary> ...
- Laravel5.1学习笔记22 Eloquent 调整修改
Eloquent: Mutators Introduction Accessors & Mutators Date Mutators Attribute Casting Introductio ...
- 阿里云机器学习tensorflow实践
1,前言 伴随人工智能和深度学习的应用越来越普及,越来越多的开发人员开始投入到智能算法的编程中.由于算法成熟且公开,软件编码这一块不存在难度:但模型训练和预测所需的时间与硬件设备的配置有极大关系,很多 ...
- GEO/SRA数据库
GEO数据库 GEO数据库隶属于NCBI,是最大最全面的基因表达数据库,主要是芯片和转录组测序数据.除储存数据外,也提供一些数据挖掘工具,因此利用好这个数据库,没有实验,没有自己的数据也能发好文章! ...