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

最新文章

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

热门文章

  1. 修改IntelliJ IDEA字体
  2. JDK版本Java SE、Java EE、Java ME的区别
  3. docker配置网络
  4. VM新安装centos7无法连接网络的问题
  5. 升级到EFCore2.0
  6. OpenStack Queens版本Horizon定制化开发
  7. Python os模块常用函数详解
  8. 2017中国大学生程序设计竞赛-哈尔滨站 H - A Simple Stone Game
  9. P2672 推销员 优先队列 + 贪心
  10. Android 自定义View消除锯齿实现图片旋转,添加边框及文字说明