1. 程式人生 > >bzoj 1083: [SCOI2005]繁忙的都市 (最小生成樹)

bzoj 1083: [SCOI2005]繁忙的都市 (最小生成樹)

實現 記得 include 最大的 pan 最小生成樹 nbsp geo 需要

鏈接:https://www.lydsy.com/JudgeOnline/problem.php?id=1083

思路:連接所有點,肯定最少是需要n-1條邊的,也就是寫個最小生成樹,記得保存下最大的權值就好了

實現代碼:

#include<bits/stdc++.h>
using namespace std;

const int M = 3e4+10;
int f[M],cnt;

struct node{
    int u,v,w;
}e[100010];

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

int Find(int x){ if(x == f[x]) return x; return f[x] = Find(f[x]); } int main() { int ans=0 ,n, m; cin>>n>>m; for(int i = 1;i <= m;i ++){ cin>>e[i].u>>e[i].v>>e[i].w; } sort(e+1,e+1+m,cmp); for(int i = 1;i <= n;i ++) f[i] = i;
for(int i = 1;i <= m;i ++){ int fx = Find(e[i].u),fy = Find(e[i].v); if(fx != fy){ f[fy] = fx; ans = e[i].w; } } cout<<n-1<<" "<<ans<<endl; }

bzoj 1083: [SCOI2005]繁忙的都市 (最小生成樹)