Prim基础最小生成树
来源:互联网 发布:网络连接4007 编辑:程序博客网 时间:2024/05/22 02:03
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102
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.
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.
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
30 990 692990 0 179692 179 011 2
Sample Output
179
模板,代码:
#include<stdio.h>#include<string.h>#define INF 1001int map[101][101],vis[101],dist[101];int n;int prim(){ int i,j,mincost=0; for(i=1;i<=n;i++) //初始化 dist[i]=map[1][i]; vis[1]=1; for(i=2;i<=n;i++) //循环n-1次,每次加入一个点 { int min=INF,p=1; for(j=1;j<=n;j++) { if(!vis[j]&&dist[j]<min) { min=dist[j]; p=j; } } vis[p]=1; //将p点加入Va中 mincost+=min; for(j=1;j<=n;j++) //对于与p相邻的在Vb集合中的j,更新到j的距离最近的点和距离 { if(!vis[j]&&dist[j]>map[p][j]) dist[j]=map[p][j]; } } return mincost;}int main(){ while(scanf("%d",&n)!=EOF) { memset(map,0,sizeof(map)); memset(vis,0,sizeof(vis)); int i,j; for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%d",&map[i][j]); int q,a,b; scanf("%d",&q); while(q--) { scanf("%d%d",&a,&b); map[a][b]=0; map[b][a]=0; } int ans=prim(); printf("%d\n",ans); } return 0;}</span>
0 0
- Prim基础最小生成树
- 算法基础 - 最小生成树(Prim算法)
- Prim最小生成树
- 最小生成树 prim
- 最小生成树 Prim
- 最小生成树-Prim
- Prim最小生成树
- 最小生成树 Prim
- prim最小生成树
- 最小生成树prim
- 最小生成树 prim
- prim 最小生成树
- Prim 最小生成树
- 最小生成树---Prim
- 最小生成树-prim
- 最小生成树---Prim
- 最小生成树(Prim)
- 最小生成树 -PRIM
- nodejs笔记
- Android开发学习笔记:数据存取之SQLite浅析
- 为什么是CarPlay
- 重构-改善既有代码的设计(1)
- oracle 类型转换函数
- Prim基础最小生成树
- QT常识
- Cookie知识
- C语言中的数据
- jq易混淆知识点
- Android编译选项eng、user、userdebug的区别
- C++模板学习
- 关于 mysql 的一些问题
- java.lang.NumberFormatException: null