hdu 1102 Constructing Roads (最小生成树)
2024-08-29 11:39:07
题目链接: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.
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.
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.
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 ;
}
最新文章
- 纪念品分组 2007年NOIP全国联赛普及组
- hdu4790 Just Random (数学?)
- WebStorm 11、PhpStorm 10免费激活(不需要注册码)
- PAT 1004. 成绩排名 (20) JAVA
- [反汇编练习] 160个CrackMe之027
- Php 输出语句
- HTML5硕士学习笔记
- Struts国际化
- 谈谈javascript 中的函数问题
- 微信小程序日历面板插件
- lambda Helper
- base64加密和解码原理和代码
- TensorFlow入门(五)多层 LSTM 通俗易懂版
- CentOS6.5安装JDK8
- GSON使用之对特殊字符的转换的处理
- hibernate显示完整的sql(转)
- 行为类模式(十):模板方法(Template Method)
- [转]android中最好的瀑布流控件PinterestLikeAdapterView
- Java 和 数据库两种方式进行加锁
- maven课程 项目管理利器-maven 1-1课程概述