POJ-2421Constructing Roads,又是最小生成树,和第八届河南省赛的引水工程惊人的相似,并查集与最小生成树的灵活与能用,水过~~~
2016-04-22 13:35
330 查看
Constructing Roads
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
Sample Output
Source
PKU Monthly,kicc
找的最小生成树专题里的一个题,看到这题却发现和河南省第八届省赛的“引水工程”惊人的相似,反正那道题也没做出来,本打算先A了那道题再做这道题,可是做了那题之后又发现这道题还更简单,于是调调代码直接A了,其实我宁愿相信是后台测试数据水的;
来说说思路吧:我们发现输入是以矩阵的模式,如果全部存在结构体中无疑是浪费内存,我们发现这个矩阵中很多相同的数,即坐标相对应的点(如a[i][j]=a[j][i])的值是一样的,所以只需要把矩阵中的一半的数存在结构体中就可以了,然后就是最简单的最小生成树了,至于有Q条已经联通好的路我们只需在输入得时候利用并查集将输入的数据的根节点合并即可;可能这里表达有点不清楚,来看代码就马上明白了;
AC代码:说白了,这题就是并查集与最小生成树的灵活运用;
Time Limit: 2000MS | Memory Limit: 65536K | |
![]() ![]() ![]() ![]() | ![]() ![]() ![]() ![]() |
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
PKU Monthly,kicc
找的最小生成树专题里的一个题,看到这题却发现和河南省第八届省赛的“引水工程”惊人的相似,反正那道题也没做出来,本打算先A了那道题再做这道题,可是做了那题之后又发现这道题还更简单,于是调调代码直接A了,其实我宁愿相信是后台测试数据水的;
来说说思路吧:我们发现输入是以矩阵的模式,如果全部存在结构体中无疑是浪费内存,我们发现这个矩阵中很多相同的数,即坐标相对应的点(如a[i][j]=a[j][i])的值是一样的,所以只需要把矩阵中的一半的数存在结构体中就可以了,然后就是最简单的最小生成树了,至于有Q条已经联通好的路我们只需在输入得时候利用并查集将输入的数据的根节点合并即可;可能这里表达有点不清楚,来看代码就马上明白了;
AC代码:说白了,这题就是并查集与最小生成树的灵活运用;
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int N=5100;//数据范围是100,平方是10000,但Q<=10000/2,开5050就可以过了; int n,m,k,f[100]; struct node { int u,v,w; } a ; int cmp(node a,node b) { return a.w<b.w; } int find(int x) { return f[x]==-1?x:x=find(f[x]); } int ks(int n) { int ans=0,cot=0; sort(a+1,a+1+k,cmp); for(int i=1; i<=k; i++) { int u=find(a[i].u); int v=find(a[i].v); if(u!=v) { ans+=a[i].w; f[u]=v; cot++; } if(cot==n-1) break; } return ans; } int main() { int x; scanf("%d",&n); k=0; memset(a,0,sizeof(a)); for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) { scanf("%d",&x); if(j>i)//把一半的数据存在结构体中; a[++k].u=i,a[k].v=j,a[k].w=x; } memset(f,-1,sizeof(f)); scanf("%d",&m); int x1,y1; while(m--) { scanf("%d%d",&x1,&y1); int xx=find(x1); int yy=find(y1); if(xx!=yy)//根节点合并->并查集; { f[xx]=yy; n--; } } printf("%d\n",ks(n)); return 0; }
相关文章推荐
- Android支付——支付宝支付总结
- JQuery中Ajax实现对Map,List集合数据的接收
- Hide Objects in Congos Connection
- Linux网络编程入门
- 避免Java内存溢出的注意事项
- C++第4次实验——输出星号图
- 0422 Step2-FCFS调度
- 三大搞笑网
- Your project contains error(s),please fix them before running your application解决办法
- Android-解决软键盘覆盖布局
- java面试题一--转载自用
- TP frame update
- js数组操作大全
- web api HttpResponseMessage的简单使用
- php简单爬虫
- SQL存储过程
- 用OpenCV建立一幅只有几个像素且值可以自定义的图像,并输出保存到硬盘的方法!
- iOS 四种存储方式通俗介绍
- IOS开发系列之UIScrollView(滚动视图)的常用属性和应用举例(滑动相册、轮播图)
- Codeforces554C组合数学+费马小定理求逆元