Connect the Cities
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24232 Accepted Submission(s): 5527
Problem Description
In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don’t want to take too much money.
Input
The first line contains the number of test cases.
Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.
Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.
Output
For each case, output the least money you need to take, if it’s impossible, just output -1.
Sample Input
16 4 31 4 22 6 12 3 53 4 332 1 22 1 33 4 5 6
Sample Output
1
Author
dandelion
Source
问题链接:HDU3371 Connect the Cities
问题描述:(略)
问题分析:
这是一个最小生成树的为问题,解决的算法有Kruskal(克鲁斯卡尔)算法和Prim(普里姆) 算法。
程序说明:
本程序使用Kruskal算法实现。有关最小生成树的问题,使用克鲁斯卡尔算法更具有优势,只需要对所有的边进行排序后处理一遍即可。程序中使用了并查集,用来判定加入一条边后会不会产生循环。程序中,图采用边列表的方式存储,排序一下就好了。
这个题需要注意存储边的结构数组的大小。
AC的C++语言程序如下:
/* HDU3371 Connect the Cities */
#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
const int N = 500;
const int N2 = 25000;
int f[N + 1], fcnt;
void UFInit(int n)
{
for(int i = 1; i <=n; i++)
f[i] = i;
fcnt = n;
}
int Find(int a) {
return a == f[a] ? a : f[a] = Find(f[a]);
}
bool Union(int a, int b)
{
a = Find(a);
b = Find(b);
if (a != b) {
f[a] = b;
fcnt--;
return true;
} else
return false;
}
struct Edge {
int u, v, w;
} edges[N2 + 1];
bool cmp(Edge& a, Edge& b)
{
return a.w < b.w;
}
int main()
{
int t, n, m, k;
scanf("%d", &t);
while(t--) {
scanf("%d%d%d", &n, &m, &k);
UFInit(n);
for(int i = 0; i < m; i++)
scanf("%d%d%d", &edges[i].u, &edges[i].v, &edges[i].w);
for(int i = 1; i <= k; i++) {
int cnt, src, dest;
scanf("%d", &cnt);
scanf("%d", &src);
cnt--;
while(cnt--) {
scanf("%d", &dest);
Union(src, dest);
}
}
// Kruscal算法
int ans = 0;
sort(edges, edges + m, cmp);
for(int i = 0; i < m; i++) {
if(Union(edges[i].u, edges[i].v))
ans += edges[i].w;
if(fcnt == 1)
break;
}
printf("%d\n", (fcnt == 1) ? ans : -1);
}
return 0;
}