UVA 10397 Connect the Campus Kruskal | | Prim

Source: Internet
Author: User

The problem with UVA is always difficult ... Will always beat around the bush, this topic gives some buildings have been built between the fiber optic cable, let the minimum spanning tree, I still prefer to use Kruskal algorithm, but this topic affirmation with prim relatively fast, prim complexity is N^2,kruskal complexity eloge.

When using Kruskal, we need to pre-use and check the set to deal with the cable that has been built, so that they belong to the same ancestor;

Use the prim algorithm when you need to set their side to 0, so that the sum of the time will not be added.

Code: (Kruskal)

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <vector> #include <set> #include <string> #include <algorithm> #define MAX 0x7fffffffusing namespace Std;struct node{int i,j;double len; Gra[640000];int p[800],n;int cmp (const void *a,const void *b) {return (node *) a)->len-((node *) b)->len >0? 1:- 1;} int find (int x) {if (x = = P[x]) return X;elsereturn p[x] = find (P[x]);} int a[800],b[800];d ouble dist (int a,int b,int c,int d) {return sqrt ((a-c) * (a-c) + (b-d) * (b-d));} void Kruskal () {int i; Double sum = 0;for (i=1; i<=n* (n-1)/2; i++) {int x = find (gra[i].i); int y = find (GRA[I].J); if (x! = y) {sum + = Gra[i].len;p [x] = y; }}//cout << sum << endl;printf ("%.2lf\n", sum);} int main () {int i,j,m,x,y;while (cin >> N) {int k = 1;for (i=1; i<=n; i++) cin >> A[i] >> b[i];for (i=1; i& lt;=n-1; i++) for (j=i+1; j<=n; J + +) {gra[k].i = I;GRA[K].J = J;gra[k].len = Dist (a[i],b[i],a[j],b[j]); k++;} CIN >> M;for (i=1; i<=n; i++) P[i] = I;for (i=1; i<=m; i++) {cin >> x >> y;int xx = find (x); int yy = f IND (y); if (xx! = yy) p[xx] = yy;} Qsort (Gra+1,k-1,sizeof (gra[0]), CMP); Kruskal ();} return 0;}
Code (PRIM):

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <vector> #include <set> #include <string> #include <algorithm> #define MAX 0x7fffffffusing namespace Std;double gra[800][800]; int M,n;int visit[800];d ouble d[800];int xx;d ouble dist (int a,int b,int c,int d) {return sqrt ((a-c) * (a-c) + (b-d) * (b-d));} V  OID Prim () {int i,j,now;double sum = 0;double min;xx = 1;d[xx] = 0; visit[xx] = 1; now = Xx;for (i=2; i<=n; i++) {for (j=1; j<=n; J + +) if (!visit[j] && d[j] > Gra[now][j]) d[j] = Gra[now][j]; min = Max;for (j=1; j<=n; j + +) if (!visit[j] && min > d[j]) min = D[now = J];visit[now] = 1;//printf ("now =%d\n ", now);//cout << min << endl;sum + = min;} printf ("%.2lf\n", sum);}  int main () {int i,j;int a[800],b[800];while (cin >> N) {for (i=1; i<=n; i++) cin >> A[i] >> b[i];for (i=1; i<=n-1; i++) for (j=i+1; j<=n; j + +) Gra[i][j] = gra[j][i] = dist (a[i],b[i],a[j],b[j]); CIN >> M;memset (visit,0,sizeof (visit)); for (i=1; i<=n; i++) d[i] = Max;int x,y;for (i=1; i<=m; i++) {cin >> X & gt;> Y;gra[x][y] = gra[y][x] = 0;} Prim ();} return 0;}



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

UVA 10397 Connect the Campus Kruskal | | Prim

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.