POJ 3625 Building Roads minimum spanning tree (prime or kruskal+ and set) (algorithm induction)

Source: Internet
Author: User
Tags acos cmath

Time limit:1000ms Memory limit:65536kb 64bit IO format:%i64d &%i64u
Description
Farmer John had just acquired several new farms! He wants to connect the farms with roads so, he can travel from any farm to any other farm via a sequence of roads; Roads already connect some of the farms.

Each of the N (1≤n≤1,000) farms (conveniently numbered 1..N) are represented by a position (Xi, Yi) on the plane (0≤x i≤1,000,000; 0≤yi≤1,000,000). Given the preexisting M roads (1≤m≤1,000) as pairs of connected farms, help Farmer John determine the smallest length Of additional roads he must build to connect all his farms.

Input
* Line 1:two space-separated integers:n and M
* Lines 2..n+1:two space-separated integers:xi and Yi
* Lines N+2..n+m+2:two space-separated integers:i and J, indicating that there is already a road connecting the farm I A nd Farm J.

Output
* Line 1:smallest length of additional roads required to connect all farms, printed without rounding to both decimal place S. is sure to calculate distances as 64-bit floating point numbers.

Sample Input
4 1
1 1
3 1
2 3
4 3
1 4
Sample Output
4.00

The meaning is simple: to give you the coordinates of the N.M......N point, and M bar has been connected to the route, the smallest spanning tree, even if each point is directly or indirectly connected. The length of the edge of the connection for the output spanning tree and ...
Very bare a problem with the smallest spanning tree ...
But here. Want to summarize the usage of the minimal spanning tree ...

There are generally two common algorithms for the minimum spanning tree. Prime and Kruskal algorithms. The difference between the two algorithms is a pair of points, a pair of edges. There is no merit or disadvantage, the complexity of prime is related to the point, while the complexity of Kruskal is related to the edge.

Kruskal is essentially a greedy thought, which is used to set and sort. First record the length of the two nodes and two points of each edge, (here with the structure), and then sort by edges, from small to large. Take it from the smallest side. If the two-node parent node is different, it is added to the edge of the minimum spanning tree. Merges the parent node of two points. Until the end. Here the order of the No-edge is again enumerated, which must be related to the number of edges in time.

poj3625: The length of the bar side of the root to be known as the edge of the spanning tree to calculate ... I just stepped on the time. 1000ms
(There are more points in this question.) No two points have to calculate the distance. The second smallest spanning tree has only n-1 edges. This can save a lot of time)

#include <cstdio>#include <cmath>#include <cstring>#include <iostream>#include <algorithm>#include <queue>#include <vector>#include <map>#include <stack>#pragma COMMENT (linker, "/stack:102400000,102400000")#define PI ACOs ( -1.0)#define EPS 1e-6#define INF (1<<24)#define MOD 1000000007using namespace STD;intfather[1005];intN,m;intpp//±?êystructpoint{intx, y;} point[1005];structlu{intx, y;DoubleDis;} lu[1000005];BOOLcmpstructLu A,structLu b) {returnA.dis-b.dis<eps;}intFindfather (intx) {if(X!=father[x]) Father[x]=findfather (father[x]);returnFATHER[X];}voidUion (intXintY) {intA=findfather (x);intB=findfather (y); Father[a]=b;}BOOLSame (intXintY) {if(Findfather (x) ==findfather (y))return true;return false;}DoubleKruskal () {intIintv=0;Doubleres=0.0; for(i=0; i<pp;i++) {if(Same (lu[i].x,lu[i].y) = =false) {uion (LU[I].X,LU[I].Y); res+=sqrt((Double) Lu[i].dis);        v++; }if(v==n-1-m) Break; }returnRes;}intMain () { while(scanf("%d%d", &n,&m)!=eof) {intI,j; for(i=1; i<=n;i++) {scanf("%d%d", &AMP;POINT[I].X,&AMP;POINT[I].Y); } pp=0; for(i=1; i<=n;i++) { for(j=i+1; j<=n;j++) {lu[pp].x=i; Lu[pp].y=j;DoubleFf= (Double) (point[i].x-point[j].x) * (point[i].x-point[j].x) + (Double) (POINT[I].Y-POINT[J].Y) * (POINT[I].Y-POINT[J].Y);                LU[PP].DIS=FF;            pp++; }} sort (lu,lu+pp,cmp); for(i=0; i<=n;i++) father[i]=i; for(i=1; i<=m;i++) {intb;scanf("%d%d", &a,&b);        Uion (A, b); }printf("%.2f\n", Kruskal ()); }return 0;}

Prime algorithm: is the adjacency matrix, without the edge of the two-point position inf (max), is also from holding up the number, constantly maintain the edge, and then with the new record point to other points of the minimum weight and ... To find the prime of the minimum spanning tree and to find the shortest Dijkstra and similarity
http://blog.csdn.net/xtulollipop/article/details/47728647
The wording is also very similar, so the two algorithms can learn together ...

It's also the top question:
I wrote it with Prime again. 79ms ... All right.. Thumped.. (for the type of the minimum spanning tree for the given point, give the edge the word.) There should be no difference between the two algorithms.

#include <cstdio>#include <cmath>#include <cstring>#include <iostream>#include <algorithm>#include <queue>#include <vector>#include <map>#include <stack>#pragma COMMENT (linker, "/stack:102400000,102400000")#define PI ACOs ( -1.0)#define EPS 1e-6#define MOD 1000000007#define INF 0x7fffffff#define INF 0x3f3f3f3f#define INF 1e7typedef Long LongLL;using namespace STD;Doublecost[1005][1005];the weight of the//side A, B. Doublemincost[1005];//x point to other points at the bottom of the weight valueBOOLused[1005];//Whether the point has been usedintN,m;structpoint{intx, y;} point[1005];DoublePrime () { for(intI=1; i<=n;i++) mincost[i]=1e7;memset(Used,false,sizeof(used)); mincost[1]=0.0;Doubleres=0.0; while(true)    {intv=-1; for(intI=1; i<=n;i++) {if(!used[i]&& (v==-1||        MINCOST[I]&LT;MINCOST[V]) v=i; }if(v==-1) Break; used[v]=true; RES+=MINCOST[V]; for(intI=1; i<=n;i++) {mincost[i]=min (mincost[i],cost[v][i]); }    }returnRes;}intMain () { while(scanf("%d%d", &n,&m)!=eof) {intI,j; for(i=1; i<=n;i++) {scanf("%d%d", &AMP;POINT[I].X,&AMP;POINT[I].Y); for(intj =1; J <= N;        ++J) Cost[i][j] = INF; } for(i=1; i<=n;i++) {cost[i][i]=0.0; for(j=i+1; j<=n;j++) {DoubleFf= (Double) (point[i].x-point[j].x) * (point[i].x-point[j].x) + (Double) (POINT[I].Y-POINT[J].Y) * (POINT[I].Y-POINT[J].Y); HfSsqrt((Double) FF);if(COST[I][J]&GT;FF) Cost[i][j]=cost[j][i]=ff; }        } for(i=1; i<=m;i++) {intb;scanf("%d%d", &a,&b); cost[a][b]=cost[b][a]=0.0; }printf("%.2f\n", Prime ()); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3625 Building Roads minimum spanning tree (prime or kruskal+ and set) (algorithm induction)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.