Luo Gu P2330 [SCOI2005] busy city minimum spanning tree

Luo Gu P2330 [SCOI2005] busy city minimum spanning tree

answer:

Topic has three conditions:

1. Those road reconstruction can put all the intersections direct or indirect communication with them.
2. In the case of 1 to meet the requirements of the transformation of the road as little as possible.
3. In the case of 1,2 to meet the requirements of those road score the biggest score of the road reconstruction as small as possible.
In fact, the nature of the third condition is the minimum spanning tree.

code show as below:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<queue>
#include<cstring>
#include<vector>
#include<map>
#define MAX 305
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

int n,m;
int fa[MAX];

int findfather(int x){
    if(x!=fa[x]){
        return fa[x]=findfather(fa[x]);
    }
    return fa[x];
}

struct edge{
    int a,b,len;
}e[100005];

bool cmp(edge a,edge b){
    return a.len<b.len;
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        fa[i]=i;
    }
    int a,b,len;
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&a,&b,&len);
        e[i].a=a,e[i].b=b,e[i].len=len;
    }
    sort(e,e+m,cmp);
    int maxl=-1;
    for(int i=0;i<m;i++){
        if(findfather(e[i].a)!=findfather(e[i].b)){
            fa[findfather(e[i].a)]=findfather(e[i].b);
            maxl=max(maxl,e[i].len);
        }
    }
    printf("%d %d",n-1,maxl);
    return 0;
}

Published 253 original articles · won praise 15 · views 7962

Guess you like

Origin blog.csdn.net/weixin_44123362/article/details/104057463