最小生成树(prim和Kruskal操!!SB题)

Posted nonames

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了最小生成树(prim和Kruskal操!!SB题)相关的知识,希望对你有一定的参考价值。

Arctic Network
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 30571   Accepted: 9220

Description

The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will in addition have a satellite channel.
Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts.

Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts.

Input

The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are integers between 0 and 10,000).

Output

For each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.

Sample Input

1
2 4
0 100
0 300
0 600
150 750

Sample Output

212.13

Source

这个sb题要注意几点:第一是memset对double类型ma初始化为INF时会出现问题。
第二点:最后输出不知道为什么这个sb题得输出非得时%.2f,%.2lf就一直wa。。。。
#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 s , n ;
double ma[609][609];
double dis[609] ;
double d[609];
int  vis[609] ;
int ans ;
struct node
    double x , y ;
a[609];

void prim()

    for(int i = 1 ; i <= n ; i++)
    
        dis[i] = ma[1][i];
    
    vis[1] = 1 ;
    for(int i = 1 ; i < n ; i++)
    
        double min = INF ;
        int pos ;
        for(int j = 1 ; j <= n ; j++)
        
            if(!vis[j] && dis[j] < min)
            
                min = dis[j];
                pos = j ;
            
        
        vis[pos] = 1 ;
        d[ans++] = min ;
        for(int j = 1 ; j <= n ; j++)
        
            if(!vis[j] && dis[j] > ma[pos][j])
            
                dis[j] = ma[pos][j];
            
        
    


bool cmp(double a , double b)

    return a > b ;


void init()

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


int main()

    int t ;
    scanf("%d" , &t);
    while(t--)
    
        scanf("%d%d" , &s , &n);
        init();
        for(int i = 1 ; i <= n ; i++)
        
            scanf("%lf%lf" , &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((a[i].y - a[j].y) * (a[i].y - a[j].y) + (a[i].x - a[j].x)*(a[i].x - a[j].x)) ;
                ma[i][j] = ma[j][i] = min(ma[i][j] , w);//这里if判断大小时,注意方向不要搞错了
            
        
        prim();
        sort(d , d + n - 1 , cmp);
        printf("%.2f\n" , d[s-1]);
    
    
    return 0 ;

 

Kruskal

#include <string.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
int  fa[20000] , ans  , n , m  , way ;
double ds[500000] ;

struct Node

    int from , to ;
    int x ;
    int y ;
    double  d;
a[500000];

bool cmp(const Node &a , const Node &b)

    return a.d < b.d ;


int gcd(int x)

    if(x == fa[x])
        return x ;
    else
        return gcd(fa[x]);



void unite(int x , int y)

    x = gcd(x) ; y = gcd(y) ;
    if(x > y) fa[x] = y ;
    else fa[y] = x ;


void init()

    for(int i = 1 ; i <= m  ; i++)
        fa[i] = i ;
        ans = 0 ;
        way = 0 ;
        memset(ds , 0 , sizeof(ds));

bool cmp1(double a , double b)

    return a > b ;


int main()

    int t ;
    cin >> t ;
    while(t--)
    
        cin >> n >> m ;
        init();
        for(int i = 1 ; i <= m ; i++)
        
            cin >> a[i].x >> a[i].y ;
        
        for(int i = 1 ; i <= m ; i++)
        
            for(int j = i + 1 ; j <= m ; j++)
            
                a[way].from = i ;
                a[way].to = j ;
                a[way].d = sqrt(pow((double)(a[i].x - a[j].x) , 2) + pow((double)(a[i].y - a[j].y) , 2));
                way++;

            
        
        sort(a , a + way , cmp) ;
        for(int i = 0 ; i < way ; i++)
        
            if( ans == m - 1)
                break ;
            if(gcd(fa[a[i].from]) != gcd(fa[a[i].to]))
            
                unite(a[i].from , a[i].to);
                ds[ans] = a[i].d;
                ans ++ ;
            
        
        sort(ds , ds + ans , cmp1);
        printf("%.2f\n" , ds[n-1]);

    

    return 0;

 

以上是关于最小生成树(prim和Kruskal操!!SB题)的主要内容,如果未能解决你的问题,请参考以下文章

Prim和Kruskal最小生成树

luoguP3366 [模板] 最小生成树

最小生成树之Kruskal算法和Prim算法

图的最小生成树算法(Prim和Kruskal)

『最小生成树』prim & Kruskal

最小生成树Prim算法和Kruskal算法