1.题目
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
2.题目大意
有N个村庄,其中i行表示几号村庄,j列表示i号村庄到达j号村庄的路程。然后对角线那里是0也可以看出来,当i==j时就是自己的村庄。
然后再一个整数Q,给出已经连接起来了的村庄。最后问你把这些村庄连起来最小的花费。
3.解题思路
最小生成树问题。那么首先建图,把 i->j 和 j ->i的距离赋值;
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=n; j++)
{
int temp;
cin>>temp;
graph[i][j] = temp;
}
}
然后把已经联通了的村庄距离改为0,因为他们不用再铺路了。
建完图之后就用prime最小生成树算出最少的权值就可以了。
4.prime模板
https://blog.csdn.net/aa793336532/article/details/71680311
可以在上面这个博客上学习一下。
void Prim(MGraph g, int v)//普利姆算法(参数:邻接矩阵,起点(即第一个生成的点,可随便取))
{
int lowcost[MAXV], closest[MAXV], i, min, j, k;
/***初始化lowcost数组,closest数组(即从起点开始设置lowcost数组,closest数组相应的值,以便后续生成使用)***/
for (i = 0; i < g.n; i++)//赋初值,即将closest数组都赋为第一个节点v,lowcost数组赋为第一个节点v到各节点的权重
{
closest[i] = v;
lowcost[i] = g.edges[v][i];//g.edges[v][i]的值指的是节点v到i节点的权重
}
/**********************************开始生成其他的节点*********************************/
for (i = 1; i < g.n; i++)//接下来找剩下的n-1个节点(g.n是图的节点个数)
{
/*****找到一个节点,该节点到已选节点中的某一个节点的权值是当前最小的*****/
min = INF;//INF表示正无穷(每查找一个节点,min都会重新更新为INF,以便获取当前最小权重的节点)
for (j = 0; j < g.n; j++)//遍历所有节点
{
if (lowcost[j] != 0 && lowcost[j] < min)//若该节点还未被选且权值小于之前遍历所得到的最小值
{
min = lowcost[j];//更新min的值
k = j;//记录当前最小权重的节点的编号
}
}
/****************输出被连接节点与连接节点,以及它们的权值***************/
printf("边(%d,%d)权为:%d\n", closest[k], k, min);
/***********更新lowcost数组,closest数组,以便生成下一个节点************/
lowcost[k] = 0;//表明k节点已被选了(作标记)
//选中一个节点完成连接之后,作数组相应的调整
for (j = 0; j < g.n; j++)//遍历所有节点
{
/* if语句条件的说明:
* (1)g.edges[k][j] != 0是指k!=j,即跳过自身的节点
* (2)g.edges[k][j]是指刚被选的节点k到节点j的权重,lowcost[j]是指之前遍历的所有节点与j节点的最小权重。若g.edges[k][j] < lowcost[j],则说明当前刚被选的节点k与节点j之间存在更小的权重,则需要更新
* (3)有人会问:为什么只跳过掉自身的节点(即k==j),而不跳过所有的已选节点?当然我们可以在if语句条件中增加跳过所有的已选节点的条件(即lowcost[j] == 0),而在本程序中我们只跳过了自身的节点?(注意:我们假设图中的边的权值大于0)但其实不是,g.edges[k][j] < lowcost[j]条件已包含跳过所有的已选节点,原因是在邻接矩阵中权值为0是最小的,即g.edges[k][j]>=0,而已选节点满足lowcost[j] == 0,则已选节点j是不满足g.edges[k][j] < lowcost[j],则会被跳过
*/
if (g.edges[k][j] != 0 && g.edges[k][j] < lowcost[j])
{
//更新lowcost数组,closest数组
lowcost[j] = g.edges[k][j];//更新权重,使其当前最小
closest[j] = k;//进入到该if语句里,说明刚选的节点k与当前节点j有更小的权重,则closest[j]的被连接节点需作修改为k
}
}
}
}
原文链接:https://blog.csdn.net/aa793336532/article/details/71680311
5.AC代码
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
#define maxn 200
const int INF =10000;
int graph[maxn][maxn], d[maxn];
int n;
void prime()
{
int i, j, k;
int lowcost[maxn];
memset(lowcost,0,sizeof(lowcost));
lowcost[1] = 1;
int min1,minid,sum=0;
for( i = 1; i<n; i++)
{
min1 = INF;
for(j = 1; j<=n; j++)
{
if(!lowcost[j] && graph[1][j] <min1)
{
min1 = graph[1][j];
minid = j;
}
}
if(min1 == INF)
break;
sum+=min1;
lowcost[minid] = 1;
for(j = 1; j <=n; j++)
{
if(!lowcost[j] && graph[1][j] > graph[minid][j])
{
graph[1][j] = graph[minid][j];
}
}
}
printf("%d\n",sum);
}
int main()
{
while(scanf("%d",&n)!= EOF)
{
for(int i= 1; i<=n; i++)
{
for(int j = 1; j<=n; j++)
{
graph[i][j] = graph[j][i] = INF;
}
}
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=n; j++)
{
int temp;
cin>>temp;
graph[i][j] = temp;
}
}
int Q; cin>>Q;
for(int i= 1; i<=Q; i++)
{
int x,y;
cin>>x>>y;
graph[x][y] = graph[y][x]=0;
}
prime();
}
return 0;
}