Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms. Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm. The distance between any two farms will not exceed 100,000.
The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
Sample Input
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
Sample Output
28
有n个农场,两两之间都有一条路径,每条路径有一定的权值。要你选择一些路径,使得所有农场互相联通可达,求选择的路径权值和最小是多少?
最小生成树模板题了。下面讲解求解最小生成树问题的常用算法Prim算法的过程。
首先我用到的几个变量 :
---------------------------------------------------
int ar[N][N;//存图,ar[i][j]=x,表示i和j的距离,则ar[i][i]=0; int dis[N];//dis[i]存的是起始点a形成的生成树中,点a到点i 还需要距离! int vis[N];//表示i节点是否在生成树中
//注意下面我可能会用点1代表点a,点2代表点b,依次类推
--------------------------------------------------
1.刚开始a只和b和d相连,所以dis[b]=dis[2]=4,dis[h]=dis[8]=8。其他点还没有和a相连,所以dis[i]=INF无穷大。
2.选择dis最小的,就是dis[2],值为4,下标为2.然后把b加入生成树中。
3.因为b加入了a的生成树中,所以和b相连的点也就和a相连了。这时候来更新他们和a点的距离。
4.因为ar[b][c]=8,ar[b][h]=11,而dis[c]=INF,dis[h]=8。选择较小的值。所以,更新后,dis[c]=8,dis[h]还是8.
5.求解过程以此类推。因为一颗n个节点的树只有n-1条边,所以这个过程只执行n-1次。
6.对了,要注意,已经加入生成树的节点不要更新也不用询问是否加入生成树中。所以我用到了vis数组。vis[i]=1表示在生成树中,vis[i]=0表示不在生成树中。
7.还有就是如果在求解过程中,得到的最小dis为INF,表明不存在最小生成树
#include<iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 105;
int n, ans;
int ar[N][N;//存图,ar[i][j]=x,表示i和j的距离,则ar[i][i]=0;
int dis[N];//dis[i]存的是起始点1形成的生成树中,点1到点i 还需要的距离
int vis[N];//表示i节点是否在生成树中
void init() {//这里是初始化
ans = 0;
memset(ar, 0, sizeof(ar));
memset(dis, 0x3f, sizeof(dis));
}
void Prim() {
memset(vis,0,sizeof(vis));
for (int i = 1; i <= n;++i) {
dis[i] = ar[1][i];
}
vis[1]=1;//起点肯定在生成树中
for (int i = 1; i <= n; ++i) {
int L = INF,m=-1;//要找到最小的dis,所以L初始赋值为无穷大,来逐步找到最小的dis,m表示下标
for (int j = 1; j <= n; ++j) {//算法 进行n-1次
if (dis[j] < L&&vis[j]==0) {//找不在生成树中的节点
L = dis[j];
m = j;
}
}
if (L != INF) {
ans += L;
}
vis[m]=1;//标记
for (int j = 1; j <= n; ++j) {
if (ar[m][j] < dis[j]&&vis[j]==0) {//更新
dis[j] = ar[m][j];
}
}
}
printf("%d\n", ans);//最小生成树的权值
}
int main() {
while (~scanf("%d", &n)) {
init();
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
scanf("%d",&ar[i][j]);
}
}
Prim();
}
return 0;
}