Constructing Roads
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 29366 Accepted: 13044
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,编号从1到N。然后是输入村庄间路径长度,以矩阵的形式输入。再输入已经建设的道路个数Q,并输入每条道路连接的两个村庄编号。要求输出需要再建设的最小路长使得所有村庄是连在一起的。
问题分析
将已经连接好的道路设置为0,就可以按最小生成树原来的方式实现了。然后每次连接记录路长就可以了。
c++程序如下
prim算法
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 105;
const int inf = 0x3f3f3f3f;
int n;
bool vis[N];
int dist[N];
int map[N][N];
int prim()//求最小生成树的权值和
{
int sum, vex, i, j;
memset(vis, 0, sizeof(vis));
sum = 0;
vex = 1;
vis[vex] = 1;
for (i = 1; i <= n; i++)
dist[i] = map[vex][i];
for (i = 1; i < n; i++)//循环次数为总点数减一
{
int ans = inf;
for (j = 1; j <= n; j++)
{
if (!vis[j] && ans>dist[j])
{
ans = dist[j];
vex = j;
if (ans == 0) break;//0是距离最小的情况,所以直接跳出循环
}
}
if (ans == inf) break;//无合适的点了,否则找到距离最短的点
vis[vex] = 1;//标记
sum += ans;//建路
for (j = 1; j <= n; j++)//更新
if (!vis[j] && dist[j] > map[vex][j]) dist[j] = map[vex][j];//与最短路略有区别
}
return sum;
}
int main()
{
scanf_s("%d", &n);
int i, j, q;
for (i = 1; i <= n;i++)
for (j = 1; j <= n; j++)
scanf_s("%d", map[i] + j);
scanf_s("%d", &q);
while (q--)
{
scanf_s("%d %d", &i, &j);
map[i][j] = map[j][i] = 0;//代表它们已经联系在了一起
}
printf("%d\n", prim());
return 0;
}
prim算法+优先队列
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 105;
int n;
int map[N][N];
bool vis[N];
struct edge
{
int vex, len;
friend bool operator<(const edge&, const edge&);
}q;
bool operator<(const edge&obj1, const edge&obj2) { return obj1.len>obj2.len; }//重载<
int prim()
{
int i, sum, vex;
priority_queue<edge>Q;//优先队列
memset(vis, 0, sizeof(vis));
vex = 1;
for (i = 1; i <= n; i++)
{
q.vex = i;
q.len = map[vex][i];
Q.push(q);
}
vis[vex] = 1;
sum = 0;
while (!Q.empty())
{
q = Q.top();//得到离生成树最短的边
Q.pop();
if (!vis[q.vex])//访问过的不再处理
{
vis[q.vex]=1;
sum += q.len;
vex = q.vex;
for (i = 1; i <= n; i++)//更新
if (!vis[i])
{
q.vex = i;
q.len = map[vex][i];
Q.push(q);
}
}
}
return sum;
}
int main()
{
int q, i, j;
scanf_s("%d", &n);
for (i = 1; i <= n;i++)
for (j = 1; j <= n; j++)
scanf_s("%d", map[i] + j);
scanf_s("%d", &q);
while (q--)
{
scanf_s("%d %d", &i, &j);
map[i][j] = map[j][i] = 0;
}
printf("%d\n", prim());
return 0;
}