有已知边的最小生成树Kruskal+Uva10397

来源:互联网 发布:工业数据采集系统 编辑:程序博客网 时间:2024/05/22 03:33

Problem E
Connect the Campus
Input:
 standard input
Output: standard output
Time Limit: 2 seconds

Many new buildings are under construction on the campus of the University of Waterloo. The university has hired bricklayers, electricians, plumbers, and a computer programmer. A computer programmer? Yes, you have been hired to ensure that each building is connected to every other building (directly or indirectly) through the campus network of communication cables.

We will treat each building as a point specified by an x-coordinate and a y-coordinate. Each communication cable connects exactly two buildings, following a straight line between the buildings. Information travels along a cable in both directions. Cables can freely cross each other, but they are only connected together at their endpoints (at buildings).

You have been given a campus map which shows the locations of all buildings and existing communication cables. You must not alter the existing cables. Determine where to install new communication cables so that all buildings are connected. Of course, the university wants you to minimize the amount of new cable that you use.

Fig: University of Waterloo Campus

 

Input

The input file describes several test case.  The description of each test case is given below:

The first line of each test case contains the number of buildings N (1<=N<=750). The buildings are labeled from 1 to N. The next N lines give the x and y coordinates of the buildings. These coordinates are integers with absolute values at most 10000. No two buildings occupy the same point. After that there is a line containing the number of existing cables M (0 <= M <= 1000) followed by M lines describing the existing cables. Each cable is represented by two integers: the building numbers which are directly connected by the cable. There is at most one cable directly connecting each pair of buildings.

Output

For each set of input, output in a single line the total length of the new cables that you plan to use, rounded to two decimal places.

Sample Input

4
103 104
104 100
104 103
100 100
1
4 2

4
103 104

104 100

104 103

100 100

1

4 2

 

Sample Output
4.41
4.41

思路:

1.kruskal,先把已知的边放到一个集合里,然后最求最小生成树。

下面是代码:

#include<iostream>#include<algorithm>#include<cstdio>#include<cmath>using namespace std;const int MAX=800;struct node{    double len;    int u,v;} edge[600000];struct A{    int x,y;} dian[1010];int N,num;int pre[MAX],rank[MAX];void init(){    num=0;    for(int i=0;i<=N;i++)    {        pre[i]=i;        rank[i]=i;    }}void add_edge(int u,int v,double x){    edge[num].u=u;    edge[num].v=v;    edge[num++].len=x;}int find(int x){    if(pre[x]==x)    return x;    return pre[x]=find(pre[x]);}void unite(int x,int y){    int tx=find(x);    int ty=find(y);    if(tx==ty)    return;    if(rank[tx]<rank[ty])    pre[tx]=ty;    else    {        pre[ty]=tx;        if(rank[tx]==rank[ty])        rank[tx]++;    }}bool cmp(node a,node b){    return a.len<b.len;}void Kruskal(){    double ans=0;    sort(edge,edge+num,cmp);    for(int i=0;i<num;i++)    {        int x=find(edge[i].u);        int y=find(edge[i].v);        if(x!=y)        {            unite(x,y);            ans+=edge[i].len;        }    }    printf("%.2lf\n",ans);}int main(){    #ifndef ONLINE_JUDGE        freopen("in.txt","r",stdin);    #endif    while(cin>>N)    {        init();        for(int i=1; i<=N; i++)        {            scanf("%d%d",&dian[i].x,&dian[i].y);            for(int j=1; j<i; j++)            {                double x=pow((dian[i].x-dian[j].x),2)+pow((dian[i].y-dian[j].y),2);                x=sqrt(x);                add_edge(i,j,x);            }        }        int cnt;        cin>>cnt;        int x,y;        for(int i=0;i<cnt;i++)        {            scanf("%d%d",&x,&y);            unite(x,y);        }        Kruskal();    }    return 0;}
2.prim或者Kruskal,把已知边的边权值设置为0.

代码如下:

#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int N = 800;const double inf = 10000000;int n, m;double x[N], y[N], map[N][N];double prim( ) {    int u;    double ans = 0, ma;    bool vis[N];    memset( vis, 0, sizeof(vis) );    vis[1] = true;    for ( int k = 2; k <= n; ++k ) {        ma = inf;        for ( int i = 1; i <= n; ++i )            if ( !vis[i] && map[1][i] < ma ) ma = map[1][i], u = i;        ans += ma;        vis[u] = true;        for ( int i = 1; i <= n; ++i )             if ( !vis[i] && map[u][i] < map[1][i] ) map[1][i] = map[u][i];    }    return ans;}int main(){    while ( scanf("%d", &n) != EOF ) {        for ( int i = 1; i <= n; ++i ) scanf("%lf%lf", &x[i], &y[i]);        for ( int i = 1; i <= n; ++i )             for ( int j = 1; j <= n; ++j ) {                if ( i != j ) map[i][j] = sqrt( (x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]) );                else map[i][j] = 99999999.0;            }        scanf("%d", &m);        for ( int i = 0; i < m; ++i ) {            int u, v;            scanf("%d%d", &u, &v);            map[u][v] = map[v][u] = 0;        }        printf("%.2lf\n", prim());    }}              


原创粉丝点击