洛谷P1991 无线通讯网
首先对题目的意思进行理解一下,可以简化为:
给出一个有p个点的图,以及每个点的坐标,你可以对s个点进行标记, 两端点都被标记的边边权为0, 最后求出在该图联通时最长边的最小值
分析一下这个题目
1.我们在求解最小生成树的时候一定从小到大选择最优的几条边,所以如果我们要使选出的边最短,该边一定在最小生成树上。
2.我们可以对s个点进行标记,让连接他们的边边权为0,此时,很显然删去边的数量越多越优,对于s个点而言,它们最多可以连s-1个边
根据分析 这个题目的题意可以被再一次简化为最小生成树上p-s+1短边
剩下就很简单了
#include<bits/stdc++.h> using namespace std; const int Maxn = 1100; const int MAxn = 250010; int s, p, m = 0, cntt = 0 , fa[Maxn]; double choose[MAxn]; struct stt { int x, y; } node[Maxn]; struct st { int u, v; double w; } edge[MAxn]; int find ( int x ) { return fa[x] == x ? x :fa[x] = find( fa[x] ); } inline double comp (const st &a, const st &b) { return a.w < b.w ; } inline double dis ( int a, int b ) { double xd = ( node[a].x - node[b].x ); double yd = ( node[a].y - node[b].y ); double anss = sqrt ( xd * xd + yd * yd ); return anss; } inline void add_edge ( int u, int v ) { edge[++m].u = u; edge[m].v = v; edge[m].w = dis( u, v ); } inline void kruscal () { int cnt = 0 ; sort(edge + 1, edge + 1 + m, comp); for ( int i = 1; i <= m; ++i ) { int uu = find ( edge[i].u ); int vv = find ( edge[i].v ); if( uu == vv ) continue ; fa[uu] = vv; choose[++cntt] = edge[i].w; if( ++cnt == p - 1 ) break ; } } int main () { scanf ( "%d %d", &s, &p ); for ( int i = 1; i <= p; ++i ) fa[i] = i; for ( int i = 1; i <= p; ++i ) scanf( "%d %d", &node[i].x, &node[i].y ); for ( int i = 1; i <= p; ++i ) for ( int j = i + 1; j <= p; ++j ) add_edge ( i, j ); kruscal () ; if ( cntt <= s ) printf ( "0.00" ); else printf ( "%.2f", choose[cntt - s + 1] ); return 0; }