【传送门:BZOJ1083】
简要题意:
一个有n个点,m条无向边的图,每条无向边都有花费,请求出最少的边使得图变成连通图,并且使得这些边中的最大花费最小,并求出最大花费
题解:
最小生成树
作为一个连通图,想都不用想第一个输出就是n-1(这个有点弱智。。)
然后直接最小生成树,然后记录最大边的花费就可以了
参考代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{
int x,y,d,next;
}a[21000];int len,last[310];
void ins(int x,int y,int d)
{
len++;
a[len].x=x;a[len].y=y;a[len].d=d;
a[len].next=last[x];last[x]=len;
}
bool cmp(node n1,node n2)
{
return n1.d<n2.d;
}
int fa[310];
int findfa(int x)
{
if(x!=fa[x]) fa[x]=findfa(fa[x]);
return fa[x];
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
len=0;memset(last,0,sizeof(last));
for(int i=1;i<=m;i++)
{
int x,y,d;
scanf("%d%d%d",&x,&y,&d);
ins(x,y,d);
}
sort(a+1,a+m+1,cmp);
for(int i=1;i<=n;i++) fa[i]=i;
int ans=0;
for(int i=1;i<=m;i++)
{
int fx=findfa(a[i].x),fy=findfa(a[i].y);
if(fx!=fy)
{
fa[fx]=fy;
ans=max(ans,a[i].d);
}
}
printf("%d %d\n",n-1,ans);
return 0;
}
来源:https://www.cnblogs.com/Never-mind/p/8444704.html