博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【最小生成树】Connect the Cities
阅读量:5045 次
发布时间:2019-06-12

本文共 3201 字,大约阅读时间需要 10 分钟。

Connect the Cities

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 13007    Accepted Submission(s): 3568

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

1

6 4 3
1 4 2
2 6 1
2 3 5
3 4 33
2 1 2
2 1 3
3 4 5 6

 

Sample Output
1
 
Author
dandelion
 题目大意:
  输入T,表示有T组数据,第一行输入N,M,K,表示有N个点,M条边,K个已经连通的集合。
  然后有M行,每行输入 A,B,C表示A-B的边权为C,
  然后有K行,每一行先输入k,表示有k个点是相互连通了的。
  问你连通这些所有点,需要的最小的边权总和是多少?
解法:Kruskal+并查集
1 #include 
2 #include
3 #include
4 #include
5 #define MAX 100005 6 using namespace std; 7 int ID[MAX];/*ID[i]=i表示i为独立点*/ 8 struct STD 9 {10 int x,y,z;11 bool operator < (const STD &p) const /*结构体自定义函数*/12 { /*结构体自定义函数*/13 return z < p.z; /*按z的大小由小到大排序 */14 }15 }stu[MAX];16 int SIGN_Num;17 void Cread(int N)18 {19 SIGN_Num=N;20 for(int i=0;i<=N;i++)ID[i]=i;21 }22 int Find(int x)/*寻找父亲节点,递归形式,有时需要栈扩展*/23 {24 int tmp;25 if(ID[x]!=x)tmp=Find(ID[x]);26 else return x;27 ID[x]=tmp; /*路径压缩*/28 return tmp;29 }30 31 int Add(int x,int y)/*添加点操作*/32 {33 x=Find(x);34 y=Find(y);35 if(x!=y)36 {37 ID[x]=y;38 return 1;39 }40 else return 0;41 }42 int Kruskal(int M)/*如果可构成最小生成树,返回最小权值*/43 { /*否则返回-1,表示无法构成最小生成树,O(N^3)*/44 int i,j,k,ii,jj;45 int Sum_Min=0,MIN;46 for(k=1;k<=M;k++)47 {48 ii=stu[k].x;49 jj=stu[k].y;50 MIN=stu[k].z;51 if(Add(ii,jj))52 {53 Sum_Min+=MIN;54 SIGN_Num--;55 }56 }57 if(SIGN_Num==1)return Sum_Min;58 return -1;59 }60 int main()61 {62 int T,i,j,k,N,M,K;63 scanf("%d",&T);64 while(T--)65 {66 scanf("%d%d%d",&N,&M,&K);67 Cread(N);68 int a,b,c;69 for(i=1;i<=M;i++)70 {71 scanf("%d%d%d",&a,&b,&c);72 stu[i].x=a;stu[i].y=b;stu[i].z=c;73 }74 sort(stu+1,stu+M+1);75 for(i=0;i
View Code

 

转载于:https://www.cnblogs.com/Wurq/articles/4562406.html

你可能感兴趣的文章
LeetCode 121 Best Time to Buy and Sell Stock
查看>>
Lambda 表达式的示例
查看>>
linux文件系统初始化过程(1)---概述
查看>>
NIO
查看>>
FASTREPORT 整理 (mtm)
查看>>
用设计精美的阅读指读应用项目源码
查看>>
Hhml 標題 閃爍
查看>>
调用系统计算器n次
查看>>
shell study
查看>>
2017-2018-1 20155220 《信息安全系统设计基础》第五周学习总结
查看>>
并发通信、生产者消费者模型
查看>>
IOS开发创建开发证书及发布App应用(二)——创建证书
查看>>
伪随机数生成方法
查看>>
windows下使用 ApiGen 生成php项目的开发文档
查看>>
好用的Android屏幕适配
查看>>
java的System.getProperty()方法可以获取的值
查看>>
mysql 安装
查看>>
JS 和 Jq 获取客户端各种屏幕宽度和高度
查看>>
2017.10.2 国庆清北 D2T1 (a*b)|x
查看>>
spring mvc json 问题
查看>>