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

原创 2013年12月04日 21:44:06

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());
    }
}
              


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

UVa 10397 Connect the Campus (Kruskal+补全最小生成树)

#include #include #include #include #include #include #include #include #include #include #include u...

UVa 10369 Arctic Network (Kruskal+最小生成树的第K小边)

#include #include #include #include #include #include #include #include #include #include #include u...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

UVA - 10397 Connect the Campus (最小生成树)

题目大意: 大学要铺设电线,在n个建筑中,使得这n个建筑,直接或间接相连,并给你了这n个建筑的坐标,还有m条已经相连的边,这些边用起点和终点表示。现在要你求怎么铺设电线才能使得总距离最小。 解析:...

uva 10397(最小生成树)

Connect the Campus Input: standard input Output: standard output Time Limit: 2 seconds Many new ...

UVa:10397 Connect the Campus(最小生成树)

题意:要你求连通所有建筑物的最小距离,但是某些建筑物已经连通。 思路:最小生成树。已经连通的建筑物可以处理一下。 #include #include #include #in...

UVa 10397 - Connect the Campus (最小生成树)

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_prob...

poj1751已知某些边建好的情况下求最小生成树中其他需要建的边

poj1751: 题意:已知某些边建好的情况下求最小生成树中其他需要建的边,输出其起点和终点 这道题目是spj,对输出的顺序没有要求 我用java写一直MLE,百思不得其解,然后又用C++写了一...

最小生成树,Kruskal(买还是建 uva 1151)

用并查集合并集合时,不能直接 p[net[t][i]]=net[t][1]; 而要 int u=net[t][i]; int v=net[t][1]; int xx=find(u); ...

UVa 10034 Freckles (最小生成树+kruskal)

#include #include #include #include #include #include #include #include #include #include #include #...

UVA1511 Buy or Build 二进制枚举+最小生成树kruskal

World Wide Networks (WWN) is a leading company that operates large telecommunication networks. WWN ...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)