题目链接: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

Problem Description
There are N villages, which are numbered from 1 to N,
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.

 
Input
The first line is an integer N (3 <= N <= 100),
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.

 
Output
You should output a line contains an integer, which is
the length of all the roads to be built such that all the villages are
connected, and this value is minimum.
 
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
 
Sample Output
179
 
题目大意:第一行表示n*n的矩阵,然后输入矩阵,表示的就是1到1的距离,1到2的距离。。。。第二行是2到1的距离,2到2的距离。。。。以此类推~
然后在输入一行,表示下面有几组数据,下面输入的两个数就是表示两个之间有路,不需要再建了,最后就是输出能够连通所有道路的最短路了!!!
 
一个最小生成树prim就可以解决了!哇哈哈~
 
 #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 (最小生成树)的更多相关文章

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

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...

  3. (step6.1.4)hdu 1102(Constructing Roads——最小生成树)

    题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好 ...

  4. HDU 1102 Constructing Roads(最小生成树,基础题)

    注意标号要减一才为下标,还有已建设的路长可置为0 题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<str ...

  5. HDU 1102 Constructing Roads, Prim+优先队列

    题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...

  6. HDU 1102(Constructing Roads)(最小生成树之prim算法)

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

  7. hdu 1102 Constructing Roads (Prim算法)

    题目连接: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. #Leetcode# 700. Search in a Binary Search Tree

    https://leetcode.com/problems/search-in-a-binary-search-tree/ Given the root node of a binary search ...

  2. xpath教程一---简单的标签搜索

    工具 Python3版本 lxml库[优点是解析快] HTML代码块[从网络中获取或者自己杜撰一个] requests[推荐安装,从网页上获取网页代码练手,再好不过了] 讲解 网页代码都是成对的标签, ...

  3. Print之modile, level

    一般print打印的design都会引入module, level. xxxprint(module, level, arg,...)... 每个Module都可以有各自的bitmap,代表这个mod ...

  4. 【Python】安装python包时遇到"error: Microsoft Visual C++ 9.0 is required"的简答

    简答 在Windows下用pip安装Scrapy报如下错误, error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall ...

  5. vijos1859[TJOI2014]电源插排

    题意:小 M 的实验室有很多电源插排.这些插排的编号从 1 到 N,由左向右排成一排.每天早晨,这些插排都是没有被使用的.每当一个学生来到实验室,他就将自己的笔记本电源插到某一个未被使用的插排上.实验 ...

  6. hdu 2722 Here We Go(relians) Again (最短路径)

    Here We Go(relians) Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  7. CentOS 转义字符

    常用转义字符 反斜杠(\):使反斜杠后面的一个变量变为单纯的字符串. 单引号(''):转义其中所有的变量为单纯的字符串. 双引号(""):保留其中的变量属性,不进行转义处理. 反引 ...

  8. 【刷题】BZOJ 1001 [BeiJing2006]狼抓兔子

    Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个 ...

  9. cf1073d Berland Fair

    ~~~题面~~~ 题解: 可以发现,每走完一圈付的钱和买的数量是有周期性的,即如果没有因为缺钱而买不起某家店的东西,那么这一圈的所以决策将会和上一圈相同,这个应该是很好理解的,想想就好了. 因为钱数固 ...

  10. 38 一次 redis 连接泄露的原因 以及 ShardedJedisPool

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u011039332/article/details/85381051前言 这个是接着 上次的 这篇文 ...