Description
The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.
Input
The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.
Output
The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.
Sample Input
9 A 2 B 12 I 25 B 3 C 10 H 40 I 8 C 2 D 18 G 55 D 1 E 44 E 2 F 60 G 38 F 0 G 1 H 35 H 1 I 35
Sample Output
216
审题:
大致意思就是,各个村庄原来有很多道路使它们互相连通,但是维护道路的成本太高,必须停用一些道路,但是又要保证各个村庄互相连通,题目已经给出各条道路的运维成本,也就是图上的权值,求一种方案,使得村庄相互连通并且维护道路的成本最低。
说的通俗一点就是求最小生成树嘛。。。
首先要注意的是读懂这个题目输入方式:
第一行是村庄的总个数n,按照字母表大写字母的顺序给各个村庄编号。然后,接下来的n-1行按顺序给出各个村庄的互通情况,以第一行为例子,第一个字母A,代表的是A村庄,然后2代表A连通两个村庄,然后接下来的B和I就是A所联通的两个村庄,这两条路的运维成本分别是12和25.
解:
我们用一个表格来存放道路的信息,每一行代表一条道路:
下标 | 村庄1 | 村庄2 | 运维成本 | 是否被并入最小生成树 |
0 | A | B | 12 | 0 |
1 | A | I | 25 | 0 |
2 | B | C | 10 | 0 |
3 | B | H | 40 | 0 |
4 | B | I | 8 | 0 |
5 | C | D | 18 | 0 |
6 | C | G | 55 | 0 |
7 | D | E | 44 | 0 |
8 | E | F | 60 | 0 |
9 | E | G | 38 | 0 |
10 | G | H | 35 | 0 |
11 | H | I | 35 | 0 |
把这个表格按照运维成本排序:
下标 | 村庄1 | 村庄2 | 运维成本 | 是否被并入最小生成树 |
0 | B | I | 8 | 0 |
1 | B | C | 10 | 0 |
2 | A | B | 12 | 0 |
3 | C | D | 18 | 0 |
4 | A | I | 25 | 0 |
5 | G | H | 35 | 0 |
6 | H | I | 35 | 0 |
7 | E | G | 38 | 0 |
8 | B | H | 40 | 0 |
9 | D | E | 44 | 0 |
10 | C | G | 55 | 0 |
11 | E | F | 60 | 0 |
然后就开始选,按顺序遍历这个数组,如果选中的边和已经并入最小生成树的边不会构成一个回路,那么证明它可以并入最小生成树。
如何判断会不会构成一个回路呢?
这里要使用并查集的知识,如果道路的起点与终点不在同一个并查集,那么这条路就不会和已有的边构成一个环。
下标 | 村庄1 | 村庄2 | 运维成本 | 是否被并入最小生成树 |
0 | B | I | 8 | 1 |
1 | B | C | 10 | 1 |
2 | A | B | 12 | 1 |
3 | C | D | 18 | 1 |
4 | A | I | 25 | 0 |
5 | G | H | 35 | 1 |
6 | H | I | 35 | 1 |
7 | E | G | 38 | 1 |
8 | B | H | 40 | 0 |
9 | D | E | 44 | 0 |
10 | C | G | 55 | 0 |
11 | E | F | 60 | 1 |
#include
using namespace std;
const int N=27;
class Node
{
public:
int a, b, w,tag;
};
void Sort(Node* graph, int i, int j)
{
if (i < j)
{
Node base = graph[i];
int low = i, high = j;
while (low < high)
{
while (low < high && graph[high].w >= base.w)
{
high--;
}
if (low < high)
{
graph[low] = graph[high];
}
while (low < high && graph[low].w <= base.w)
{
low++;
}
if (low < high)
graph[high] = graph[low];
}
graph[low] = base;
Sort(graph, i, low - 1);
Sort(graph, low + 1, j);
}
}
int input(int n,Node *graph)
{
int k=0;
for (int i = 0; i < n - 1; i++)
{
char ch1,ch2;
int temp;
cin >> ch1 >> temp;
if (temp == 0)
{
continue;
}
for (int j = 0; j < temp; j++)
{
int w;
cin >> ch2 >> w;
graph[k].a = ch1-'A';
graph[k].b = ch2-'A';
graph[k++].w = w;
}
}
return k;
}
bool judge(int a, int b, int *aa)//判断可不可以并入最小生成树
{
int s1 = a, s2 = b;
while (aa[s1] != -1)
{
s1 = aa[s1];
}
while (aa[s2] != -1)
{
s2 = aa[s2];
}
if (s1 == s2)
return false;
else
return true;
}
int kruscal(int mfs[][N], int roads, Node graph[], int n)
{
int a[N];//并查集
for (int i = 0; i < N; i++)
{
a[i] = -1;
}
int num = 0;
for (int i = 0; i < roads; i++)
{
if (num == n - 1)
break;
if(judge(graph[i].a,graph[i].b,a))
{
int t1 = graph[i].a, t2 = graph[i].b;
while (a[t1] != -1)
t1 = a[t1];
while (a[t2] != -1)
t2 = a[t2];
if (t1 < t2)
{
a[t2] = t1;
}
else
{
a[t1] = t2;
}
mfs[graph[i].a][graph[i].b] = graph[i].w;
mfs[graph[i].b][graph[i].a] = graph[i].w;
num++;
}
}
int sum = 0;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (mfs[i][j] != 0)
sum += mfs[i][j];
}
}
return sum;
}
int main()
{
Node graph[10*N];
int n;
cin >> n;
int roads=input(n,graph);
Sort(graph, 0, roads-1);
int mfs[N][N] = {0};
cout<
————————————————————————
看了一下自己以前写的代码,无力吐槽。。。
放一个最近写的版本
#include
#include
using namespace std;
int n, m, f[51];
struct Edge {
int u, v, w;
bool operator<(const Edge& e)const {
return w < e.w;
}
}e[51];
//路径压缩式查询
int find(int x) {
if (f[x] == 0)
return x;
return f[x] = find(f[x]);
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> e[i].u >> e[i].v >> e[i].w;
}
sort(e + 1, e + 1 + m);
int cnt = 0, ans = 0;
for (int i = 1; i <= m && cnt < n - 1; i++) {
//利用并查集判断有无回路
int v1 = find(e[i].u), v2 = find(e[i].v);
if (v1 != v2) {
if (v1 < v2) {
f[e[i].v] = v1;
}
else {
f[e[i].u] = v2;
}
++cnt, ans += e[i].w;
}
}
cout << ans;
return 0;
}