博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU1102--Constructing Roads
阅读量:5216 次
发布时间:2019-06-14

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

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10598 Accepted Submission(s): 3942
Problem 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
Source
kicc
Recommend
Eddy

简单最小生成树

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 9 int parent[5001]; 10 int n; 11 int edge[1001][1001]; 12 13 struct node 14 { 15 int u,v; 16 int w; 17 }point[5001]; 18 19 bool cmp(node a,node b) 20 { 21 return a.w
0;s=parent[s]){} 37 while(s!=x) 38 { 39 int temp=parent[x]; 40 parent[x]=s; 41 x=temp; 42 } 43 return s; 44 } 45 46 void Union(int R1,int R2) 47 { 48 int r1=Find(R1),r2=Find(R2); 49 int temp=parent[r1]+parent[r2]; 50 if(parent[r1]>parent[r2]) 51 { 52 parent[r1]=r2; 53 parent[r2]=temp; 54 } 55 else 56 { 57 parent[r1]=temp; 58 parent[r2]=r1; 59 } 60 } 61 62 int kruskal(int x) 63 { 64 int sum=0; 65 int i; 66 for(i=1;i
i) 92 { 93 point[cnt].u=i; 94 point[cnt].v=j; 95 point[cnt++].w=edge[i][j]; 96 } 97 } 98 } 99 sort(point+1,point+cnt,cmp);100 int q;101 scanf("%d",&q);102 for(i=1;i<=q;i++)103 {104 int u,v;105 scanf("%d%d",&u,&v);106 if(Find(u)!=Find(v))107 {108 Union(u,v);109 }110 }111 int ans=kruskal(cnt);112 printf("%d\n",ans);113 }114 return 0;115 }
View Code

 

 

转载于:https://www.cnblogs.com/zafuacm/p/3199712.html

你可能感兴趣的文章
vue中router与route的区别
查看>>
js 时间对象方法
查看>>
网络请求返回HTTP状态码(404,400,500)
查看>>
Spring的JdbcTemplate、NamedParameterJdbcTemplate、SimpleJdbcTemplate
查看>>
Mac下使用crontab来实现定时任务
查看>>
303. Range Sum Query - Immutable
查看>>
迪杰斯特拉算法---单源点最短路径
查看>>
【python】TCP/IP编程
查看>>
JVM 类型的生命周期学习
查看>>
图片加载失败显示默认图片占位符
查看>>
2018 ZJCPC
查看>>
【★】浅谈计算机与随机数
查看>>
[转载]宇宙文明等级的划分标准
查看>>
Jmeter的log输出控制
查看>>
《代码阅读方法与实现》阅读笔记一
查看>>
ActiveMQ配置使用 for CentOS6
查看>>
解决 sublime text3 运行python文件无法input的问题
查看>>
javascript面相对象编程,封装与继承
查看>>
linux下配置固定ip
查看>>
MsSql 游标 修改字段两个表关联 表向另个表插入记录
查看>>