当前位置 : 主页 > 大数据 > 区块链 >

最小生成树(prim和Kruskal)

来源:互联网 收集:自由互联 发布时间:2021-06-22
Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26102 Accepted: 7644 Special Judge Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flato
Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26102   Accepted: 7644   Special Judge

Description

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns. However, there are still some towns that you can‘t reach via a highway. It is necessary to build more highways so that it will be possible to drive between any pair of towns without leaving the highway system.

Flatopian towns are numbered from 1 to N and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus their length is equal to Cartesian distance between towns. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.

The Flatopian government wants to minimize the cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length. Thus, the least expensive highway system will be the one that minimizes the total highways length.

Input

The input consists of two parts. The first part describes all towns in the country, and the second part describes all of the highways that have already been built.

The first line of the input file contains a single integer N (1 <= N <= 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of i th town (for i from 1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location.

The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a highway. Each pair of towns is connected by at most one highway.

Output

Write to the output a single line for each new highway that should be built in order to connect all towns with minimal possible total length of new highways. Each highway should be presented by printing town numbers that this highway connects, separated by a space.

If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty.

Sample Input

9
1 5
0 0 
3 2
4 5
5 1
0 4
5 2
1 2
5 3
3
1 3
9 7
1 2

Sample Output

1 6
3 7
4 9
5 7
8 3
题意:求最小生成树的具体哪两个节点。给了一些边和节点,求最小生成树的新的需要的节点,

Kruskal:自己的一顿骚操作把自己给搞死。。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <map>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 1024
using namespace std;
typedef long long ll ;
int n , m ;
int ans ;
int fa[809];

struct node
{
    int x , y ;
}a[809];

struct node1{
    int from , to ;
    double w ;
}ma[300009];

void init()
{

    memset(vis , 0 , sizeof(vis));
    ans = 0 ;
    for(int i = 1 ; i <= n ; i++)
        fa[i] = i ;

}

int find(int x)
{
    return x == fa[x] ? x : find(fa[x]);
}


void unite(int u , int v)
{
    u = find(u) , v = find(v);
    if(u > v) fa[u] = v ;
    else
        fa[v] = u ;
}

bool cmp(node1 a , node1 b)
{
    return a.w < b.w ;
}

int main()
{
    scanf("%d" , &n);
    init();
    for(int i = 1 ; i <= n ; i++)
    {
       scanf("%d%d" , &a[i].x , &a[i].y);
    }
    //连通网
    int way = 0 ;
    for(int i = 1 ; i <= n ; i++)
    {
        for(int j = i + 1 ; j <= n ; j++)
        {
            double w ;
            w = sqrt(pow(a[i].x - a[j].x , 2) + pow(a[i].y - a[j].y , 2));
            ma[way].from = i , ma[way].to = j , ma[way].w = w ;
            way ++ ;
        }
    }
    scanf("%d" , &m);
    for(int i = 1 ; i <= m ; i++)
    {
        int u , v ;
        scanf("%d%d" , &u , &v);
        if(find(fa[u]) != find(fa[v]))
        {
            unite(u , v);
            ans++ ;
        }

    }
    sort(ma , ma + way , cmp);
    for(int j = 0 ; j < way ; j++)
    {
        if(find(fa[ma[j].from]) != find(fa[ma[j].to]))
        {
            unite(ma[j].from , ma[j].to);
            printf("%d %d\n" , ma[j].from , ma[j].to);
            ans++;
        }
        if(ans == n - 1)
            break ;
    }

    return 0 ;
}

 

prim:有几个比较妙的处理:1、将已存在的边权赋值为0,2、新开一个p数组记录每条最优边。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <map>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 1024
using namespace std;
typedef long long ll ;
double ma[809][809];
int n , m ;
int vis[809];
double dis[809];
int p[809];
int ans ;

struct node
{
    int x , y ;
}a[809];

void init()
{
    for(int i = 1 ; i <= n ; i++)
        for(int j = 1 ; j <= n ; j++)
            if(i == j)
                ma[i][i] = 0;
            else
                ma[i][j] = INF;
    memset(vis , 0 , sizeof(vis));
    ans = 0 ;

}

void Dijia()
{
    for(int i = 1 ; i <= n ; i++)
    {
        dis[i] = ma[1][i];
        p[i] = 1 ;//初始为所有城镇与1连接为最优,与dis数组相似
    }
    vis[1] = 1 ;

    for(int i = 1 ; i < n ; i++)
    {
        double min1 = INF ;
        int pos ;
        for(int j = 1 ; j <= n ; j++)
        {
            if(!vis[j] && dis[j] < min1)
            {
                min1 = dis[j];
                pos = j ;
            }
        }
        vis[pos] = 1 ;
        for(int j = 1 ; j <= n ; j++)
        {
            if(!vis[j] && dis[j] > ma[pos][j])
            {
                dis[j] = ma[pos][j];
                p[j] = pos ;//当有更优的路线到j城镇更新距离,更新与j城镇相连的城镇号
            }
        }
    }
}

int main()
{

    scanf("%d" , &n);
    init();
    for(int i = 1 ; i <= n ; i++)
    {
       scanf("%d%d" , &a[i].x , &a[i].y);
    }
    //连通网
    for(int i = 1 ; i <= n ; i++)
    {
        for(int j = i + 1 ; j <= n ; j++)
        {
            double w ;
            w = sqrt(pow(a[i].x - a[j].x , 2) + pow(a[i].y - a[j].y , 2));
            ma[j][i] = ma[i][j] = min(ma[i][j] , w);
        }
    }
    scanf("%d" , &m);
    for(int i = 1 ; i <= m ; i++)
    {
        int u , v ;
        scanf("%d%d" , &u , &v);
        ma[u][v] = ma[v][u] = 0 ;//将存在的道路间的距离赋值为0
    }
    Dijia();
    for(int i = 1 ; i <= n ; i++)
    {
        if(ma[i][p[i]] != 0)
            printf("%d %d\n" , i , p[i]);
    }

    return 0 ;
}
网友评论