链接:点击打开链接
两种通信方式:一种P个节点的mst,另一种从mst上选S个节点直接通信。
当S为2的时候可以直接想到 使最长边所连接的两个点直接通信即可,但是S为3呢或者说选最长的两条边就会涉及到好多种情况。。。
换一个角度,P个节点 P-1条边的mst,一个强连通分量,每去除一条边增加一个强连通,这样的话只要每个强连通上选取一个点就行了。所以prim求出所有边排序或者kruskal加入边。
以下是prim代码。。
</pre><pre name="code" class="html">
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <map>
#include <vector>
#include <math.h>
using namespace std;
int N, P;
struct node {
double x, y;
};
node loc[555];
double mat[555][555];
double edge[555];
void prim()
{
bool vis[555]={0};
double dis[555];
for(int i=2; i<=P; i++) dis[i]=mat[1][i]; dis[1]=0; vis[1]=1;
for(int i=1; i<P; i++)
{
int k=0; double mi=1e8;
for(int j=2; j<=P; j++)
{
if(!vis[j] && dis[j]<mi) k=j, mi=dis[j];
}
edge[i]=mi;
vis[k]=1;
for(int j=2; j<=P; j++)
{
if(!vis[j] && dis[j]>mat[k][j]) dis[j]=mat[k][j];
}
}
}
int main()
{
int T; cin>>T;
while(T--)
{
cin>>N>>P;
for(int i=1; i<=P; i++)
{
scanf("%lf%lf", &loc[i].x, &loc[i].y);
for(int j=1; j<i; j++)
{
mat[i][j] = mat[j][i] = sqrt((loc[i].x-loc[j].x)*(loc[i].x-loc[j].x)+(loc[i].y-loc[j].y)*(loc[i].y-loc[j].y));
}
}
prim();
sort(edge+1, edge+P);
//for(int i=1;i<P;i++) printf("%lf ", edge[i]); putchar('\n');
printf("%.2lf\n", edge[P-N]);
}
return 0;
}