HDU3371-最小生成树-Kruskal-Prim

来源:互联网 发布:用友u8数据库下载 编辑:程序博客网 时间:2024/05/22 15:09

Connect the Cities

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8056    Accepted Submission(s): 2293

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.
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
HDOJ Monthly Contest – 2010.04.04
 
解释:
        题目意思:有一些城市,其中有一些城市之间的路未通,已知修某些路径需要一定的花费,而有一些路径已经连通(不需再花钱修理),求为使得所有城市连通,
至少需要花费多少钱。
        裸的最小生成树算法:Prim或者Kruskal都可以。
Prim: 1.注意重边!2.注意图并不是连通图(有多个分支)
#include <stdio.h>#include <string>using namespace std;const int INF = 0x7f7f7f7f;int map[505][505];int dist[505];int con[505];bool visited[505];int Prim(int n){int i, j, min, pos, sum = 0;memset(visited,false,sizeof(visited));memset(dist,0x7f,sizeof(dist));dist[1] = 0;for (i=1; i <= n; ++i){min = INF, pos = 1;for (j=1; j <= n; ++j){if (!visited[j] && dist[j] < min){min = dist[j], pos = j;}}if (min == INF){return -1;}sum += min;visited[pos] = true;for (j=1; j <= n; ++j){if (!visited[j] && map[pos][j] < dist[j]){dist[j] = map[pos][j];}}}return sum;}int main(){#ifndef ONLINE_JUDGEfreopen("2.txt","r",stdin);#endifint T,n,m,k,i,j,a,b,w,p;scanf("%d",&T);while (T--){memset(map,0x7f,sizeof map);scanf("%d%d%d",&n,&m,&k);while (m--){scanf("%d%d%d",&a,&b,&w);if (w < map[a][b]){map[a][b] = map[b][a] = w;}}while (k--){scanf("%d",&p);for (i=0; i < p; ++i){scanf("%d",con+i);}for (i=0; i < p; ++i){for (j=i+1; j < p; ++j){map[con[i]][con[j]] = map[con[j]][con[i]] = 0;}}}printf("%d\n",Prim(n));}return 0;}
Kruskal:1注意重边(不处理重边也没有关系,因为如果存在重边,Kruskal会优先选择最短的一条边)2.处理是否是连通图
#include <iostream>#include <string>#include <algorithm>#include <limits.h>using namespace std;struct edge{int beg;int end;int cost;} Edge[25005];int father[505];int Num[505];int n,m,cost;bool cmp(const edge& x,const edge& y){return x.cost < y.cost;}void Init(){for (int i=1; i <= n; ++i){father[i] = i;}}int Find(int x){if (x==father[x]){return x;}else{return father[x] = Find(father[x]);}}void Union(int x,int y,int w){x = Find(x);y = Find(y);if (x != y){father[x] = y;cost += w;}}void Kruskal(){cost = 0;sort(Edge,Edge+m,cmp);for (int i=0; i < m; ++i){Union(Edge[i].beg,Edge[i].end,Edge[i].cost);}}int main(){#ifndef ONLINE_JUDGEfreopen("2.txt","r",stdin);#endifint T,k,i,j,num,root;scanf("%d",&T);while (T--){scanf("%d%d%d",&n,&m,&k);for (i=0; i < m; ++i){scanf("%d%d%d",&Edge[i].beg,&Edge[i].end,&Edge[i].cost);}cost = 0;Init();for (i=0; i < k; ++i){scanf("%d",&num);//num个点的路已修好,说明num个点之间的费用为0,用num-1条权值为0的边连起它们即可for (j=0; j < num; ++j){scanf("%d",&Num[j]);}for (j=1; j < num; ++j){Union(Num[0],Num[j],0);}}Kruskal();root = 0;for (i=1; i <= n; ++i){if (i==father[i]){++root;}if (root > 1)break;}if (root > 1){puts("-1");}else{printf("%d\n",cost);}}return 0;}


0 0