hdu 1102 Constructing Roads (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14172 Accepted Submission(s):
5402
and you should build some roads such that every two villages can connect to each
other. We say two village A and B are connected, if and only if there is a road
between A and B, or there exists a village C such that there is a road between A
and C, and C and B are connected.
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.
which is the number of villages. Then come N lines, the i-th of which contains N
integers, and the j-th of these N integers is the distance (the distance should
be an integer within [1, 1000]) between village i and village j.
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.
the length of all the roads to be built such that all the villages are
connected, and this value is minimum.
#include <iostream>
#include <cstdio>
using namespace std;
int map[][],node[],Min,n,a,b;
const int INF=; int prim()
{
int vis[]= {};
int tm=,m,s=;
vis[tm]=;
node[tm]=;
for (int k=; k<=n; k++)
{
Min=INF;
for (int i=; i<=n; i++)
if (!vis[i])
{
if (node[i]>map[tm][i])
node[i]=map[tm][i];
if (Min>node[i])
{
Min=node[i];
m=i;
}
//s+=Min;
}
tm=m;
vis[m]=; }
for (int i=; i<=n; i++)
s+=node[i];
return s;
} int main ()
{
while (cin>>n)
{
//cout<<n<<endl;
for (int i=; i<=n; i++)
{
node[i]=INF;
for (int j=; j<=n; j++)
map[i][j]=INF;
}
for (int i=; i<=n; i++)
{
for (int j=; j<=n; j++)
{
cin>>map[i][j];
}
}
int q;
cin>>q;
while (q--)
{
cin>>a>>b;
map[a][b]=map[b][a]=;
}
printf ("%d\n",prim());
}
return ;
}
hdu 1102 Constructing Roads (最小生成树)的更多相关文章
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- hdu 1102 Constructing Roads(最小生成树 Prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...
- (step6.1.4)hdu 1102(Constructing Roads——最小生成树)
题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好 ...
- HDU 1102 Constructing Roads(最小生成树,基础题)
注意标号要减一才为下标,还有已建设的路长可置为0 题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<str ...
- 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 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 ...
随机推荐
- sublime text 输入法不跟随光标
1.引子 sublime text 有个BUG,那就是不支持中文的鼠标跟随(和PS类似输入的光标和文字候选框不在一起).如下图: 2.插件 安装IMESupport插件即可插件,这款插件是日本人写的. ...
- JXM 监控tomcat 7(含代码
1.在tomcat的server.xml中加入: <Listener className="org.apache.catalina.mbeans.JmxRemoteLifecycleL ...
- C# 压缩组件介绍与入门
1.前言 作为吉日嘎拉权限管理系统最早的一批学习版用户,学了不少东西,在群里面也结识了很多朋友,更重要的是闲余时间,大家都发布很多可靠的外包工作.这次也是由于吉日嘎拉发布了一个有关“压缩文件损坏检测” ...
- CURL & Fetch
CURL & Fetch https://kigiri.github.io/fetch/ https://stackoverflow.com/questions/31039629/conver ...
- CMD (sea.js)模块定义规范
转自http://www.cnblogs.com/hongchenok/p/3685677.html CMD 模块定义规范 在 Sea.js 中,所有 JavaScript 模块都遵循 CMD(C ...
- hdu2295-Radar
有n个城市,\(m\)个雷达,\(k\)个操作员,每个操作员只能操作一个雷达.每个雷达的覆盖范围是一个以雷达坐标为中心的圆,所有雷达的覆盖半径是相同的. 现在给出这\(n\)个城市,\(m\)个雷达的 ...
- echart模块化单文件引入
echart模块化单文件引入百度上面是推荐这样使用.今天看了一下,做了个Demo. 文件结构如下:
- symbol lookup error *** , undefined symbol 错误
在重装samba过程后遇到一些问题,使用 gdb 时产生报错: gdb: symbol lookup error: gdb: undefined symbol: PyUnicodeUCS2_FromE ...
- zlog使用手册
zlog使用手册 来源 http://hardysimpson.github.io/zlog/UsersGuide-CN.html Contents Chapter 1 zlog是什么? 1.1 ...
- 【刷题】洛谷 P2709 小B的询问
题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...