EOJ2067 minimum Spanning Tree

Source: Internet
Author: User

EOJ2067 minimum spanning tree prime algorithm and Kruskal algorithm implementation

Topic:

Building Roads

Time limit:1000ms Memory limit:30000kb
Total submit:476 accepted:144

description

Farmer John had just Acquired several new farms! He wants to connect the Farmswith roads so, he can travel from any farm to any other farm via a sequenceof roads; Roads already connect some of the farms.

Each of the N (1 <= n <= $) farms (conveniently numbered 1..N) isrepresented by a position (x_i, y_i) on The plane (0 <= x_i <=1,000,000;0 <= y_i <= 1,000,000). Given the preexisting M roads (1 <= m <=1,000) as pairs of connected farms, help Farmer John
determine the Smalles T length of additional roads he must build to connect Allhis farms.

Input

* Line 1:two space-separated integers:n and M
* Lines 2..n+1:two space-separated integers:x_i and Y_i
* Lines N+2..n+m+2:two space-separated integers:i and J, indicating Thatthere is already a road connecting the farm I an D Farm J.

Output

4 1
1 1
3 1
2 3
4 3
1 4

INPUT DETAILS:
Four farms at locations (3,1), (2,3), and (4,3). Farms 1 and 4 areconnected by a road.

Sample Input

* Line 1:smallest length of additional roads required to connect allfarms,printed without rounding to the decimal places. Be sure to calculate distancesas 64-bit floating point numbers.

Sample Output

4.00

OUTPUT DETAILS:
Connect Farms 1 and 2 with a road that's 2.00 units long, then connect Farms 3and 4 with a road that's 2.00 units long. The best we can does, andgives us a total of 4.00 unit lengths.

Topic Analysis:

Given some points of the coordinates, to build roads between these points, some points between the road has been repaired, to find the shortest road length, make the diagram connected, the minimum generation of tree exercises. The distance between a bit is preprocessed, and the distance between the repaired points is changed to 0. Next, the prime algorithm and the Kruskal algorithm can be implemented. The 1.prime algorithm is similar to the Dijkstra algorithm, each time it is selected to the shortest point of the spanning tree being built into the tree construction, and updates the minimum distance between this collection and the points outside the collection with the newly added points. The 2.kruskal algorithm takes the shortest edge each time to build, and can not appear loop.

Comparing two algorithms, it is found that the prime algorithm is more efficient because the Kruskal complex graph has a log (N*n) constant more than the prime algorithm when the graph is close to the full graph.

The two implementation codes are as follows:

1. Prime algorithm:

#include <iostream>

#include <cstdio>

#include <cstring>

#include <cmath>

using namespace Std;

struct node{

Double x, y;

}NODE[1005];

Double cost[1005][1005];

Double low[1005];

BOOL vis[1005];

Double Dis (Node a,node b) {

Returnsqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));

}

Double prime (int n) {

int i,j,k;

Double ans=0.0;

Vis[1]=true;

for (I=2;i<=n;++i)

Low[i]=cost[1][i];

for (I=1;i<=n-1;++i) {

Double minn=1000000000;

for (J=1;J<=N;++J) {

if (!vis[j] &&minn>low[j]) {

Minn=low[j];k=j;

}

}

Vis[k]=true;

ANS+=LOW[K];

for (J=1;J<=N;++J) {

if (!vis[j]) {

Low[j]=min (Low[j],cost[k][j]);

}

}

}

return ans;

}

int main ()

{

int n,m,i,j;

memset (vis,false,sizeof (VIS));

scanf ("%d%d", &n,&m);

for (I=1;i<=n;++i)

scanf ("%lf%lf", &node[i].x,&node[i].y);

for (I=1;i<=n;++i) {

for (J=I+1;J<=N;++J) {

Cost[j][i]=cost[i][j]=dis (Node[i],node[j]);

}

}

for (I=0;i<m;++i)

{

int x, y;

scanf ("%d%d", &x,&y);

cost[x][y]=cost[y][x]=0.0;

}

Double Ans=prime (n);

printf ("%.2f\n", ans);

return 0;

}

2. Kruscal algorithm:

#include <iostream>

#include <cstdio>

#include <cmath>

#include <algorithm>

using namespace Std;

struct node{

Double x, y;

}NODE[1005];

struct e{

int pt1,pt2;

Double length;

}E[1005*1005/2];

int set[1005];

int find (int x) {

Returnx==set[x]?x: (Set[x]=find (set[x]));

}

Double Dis (Node a,node b) {

Return (a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y);

}

BOOL CMP (const e& A,const e& b) {

Return a.length<b.length;

}

Double Kruskal (int n,int k) {

Double ans=0.0;

int cnt=0,i;

for (I=0;i<k;++i) {

Intfx=find (E[I].PT1), Fy=find (E[I].PT2);

if (fx==fy) continue;

Set[fx]=fy;

ANS+=SQRT (e[i].length);

++cnt;

if (cnt==n-1) break;

}

return ans;

}

int main ()

{

int n,m,i,j,k=0;

scanf ("%d%d", &n,&m);

for (I=1;i<=n;++i)

scanf ("%lf%lf", &node[i].x,&node[i].y);

for (I=1;i<=n;++i) {

for (J=1;J<=I-1;++J) {

E[k].pt1=i;

E[k].pt2=j;

E[k++].length=dis (Node[i],node[j]);

}

}

for (I=0;i<m;++i)

{

int x, y;

scanf ("%d%d", &x,&y);

if (x<y) swap (x, y);

cout<< (x-2) * (x-1)/2+y-1<<endl;

e[(x-2) * (x-1)/2+y-1].length=0.0;

}

Sort (e,e+k,cmp);

for (i=1;i<1005;++i) set[i]=i;

Double Ans=kruskal (n,k);

printf ("%.2f\n", ans);

return 0;

}

EOJ2067 minimum Spanning Tree

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.