HDU 1102 Constructing Roads "simple minimum spanning tree, prime algorithm +kruskal algorithm"

Source: Internet
Author: User


Constructing RoadsTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 20765 Accepted Submission (s): 7934


Problem Descriptionthere is N villages, which is numbered from 1 to N, and we should build some roads such that every t Wo villages can connect to each of the other. We say 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 was a road between A and C, and C and B are connected.

We know that there be already some roads between some villages and your job is the build some roads such so all the Vil Lages is connect and the length of the roads built is minimum.

Inputthe first line is a integer n (3 <= N <=), 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 are the distance (the distance s Hould is an integer within [1, +]) between village I and village J.

Then there was an integer q (0 <= q <= n * (n + 1)/2). Then come Q lines, each line contains the integers a and B (1 <= a < b <= N), which means the road between Villag e A and village B has been built.

Outputyou should output a line contains an integer, which is the length of the "All the roads" to be built such Lages is connected, and this value is minimum.

Sample Input
30 990 692990 0 179692 179 011 2

Sample Output
179

Sourcekicc
original title link: http://acm.hdu.edu.cn/showproblem.php?pid=1102

Direct set of templates.


Prime Algorithm AC code: 15MS

#include <cstdio> #include <cstring> #include <iostream>using namespace std;const int inf=0x3f3f3f3f;        int a[105][105];int dis[105];bool vis[105];int n;int Prime () {for (int i=1;i<=n;i++) {dis[i]=a[1][i];    Vis[i]=false;    } dis[1]=0;    Vis[1]=true;    int ans=0;        for (int i=1;i<n;i++) {int minn=inf;        int p=-1;        for (int j=1;j<=n;j++) {if (!vis[j]&&dis[j]<minn) minn=dis[p=j];        }//if (P==-1)//return-1;        Ans+=minn;        Vis[p]=true;        for (int j=1;j<=n;j++) {if (!vis[j]&&dis[j]>a[p][j]) dis[j]=a[p][j]; }} return ans; int main () {while (scanf ("%d", &n)!=eof) {for (int. i=1;i<=n;i++) {for (int j=1;j<=n        ; j + +) scanf ("%d", &a[i][j]);        } int t,x,y;        scanf ("%d", &t); while (t--) {scanf ("%d%d", &x,&y);        a[x][y]=a[y][x]=0;    } printf ("%d\n", Prime ()); } return 0;}

Kruskal Algorithm AC code: 46MS

#include <cstdio> #include <algorithm>using namespace std;struct node{int s,e,w;}    A[5000];int n,m;int fa[105];int Find (int x) {if (x==fa[x]) return x; Return Fa[x]=find (Fa[x]);} BOOL CMP (node A,node b) {return A.W&LT;B.W;}    int Kruskal () {sort (a,a+m,cmp);    int ans=0;        for (int i=0;i<m;i++) {int fx=find (A[I].S);        int Fy=find (A[I].E);            if (fx!=fy) {ANS+=A[I].W;        Fa[fx]=fy; }} return ans;        int main () {while (scanf ("%d", &n)!=eof) {m=0;        int t;                for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {scanf ("%d", &t);                    if (i<j) {a[m].s=i;                    A[m].e=j;                a[m++].w=t;        }}} for (int i=1;i<=n;i++) fa[i]=i;        scanf ("%d", &t);        int x, y; while (t--) {scanf ("%d%d ", &x,&y);            int Fx=find (x);            int Fy=find (y);        if (fx!=fy) fa[fx]=fy;    } printf ("%d\n", Kruskal ()); } return 0;}



HDU 1102 Constructing Roads "simple minimum spanning tree, prime algorithm +kruskal algorithm"

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.