【POJ 2349 最小生成树】
来源:互联网 发布:p2p网贷网站源码 编辑:程序博客网 时间:2024/06/05 05:58
http://poj.org/problem?id=2349
/*
poj_2349 最小生成树
这题其实最让我纠结的是卫星数。前面都是套模板的,然后记录树每两点之间的距离。
看到网上每个人都说去掉最长的s-1条边剩下最长的就是了。想了一阵子才明白。。
其实卫星数就是连通分支的个数,有多少个卫星,就可以有多少个连通分支。这样每去
掉一条边,我们就多了一个分支。那么我们就可以去掉s-1条最长边,得到s个连通分支。
(每个分支里有一个卫星)剩下的最长边则为所求了。
*/
题意:有P个点,用坐标给出,有两种联系方式:1每个点可以和距离在D以内的点相互联系,2有S个专门的卫星通道,两个点直接联系;
求D最小多少可以把这个图连起来
题解:首先不考虑S个卫星通道,先求最小生成树,用卫星通道把最小生成树中最大的S-1个边代替掉,然后剩下的最大的那条边的值
就是能把整个图连起来的D的最小值
#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>const int MAXN=505;using namespace std;int father[MAXN]; struct Edge{ int s,e; int visited; double length;}edge[MAXN*MAXN];struct p{ int x,y;}point[MAXN];int Count;bool cmp(Edge x,Edge y){ return x.length<y.length;}int ifind(int x){ if(x==father[x]) return x; father[x]=ifind(father[x]); return father[x];}bool Union( int x , int y ){ int f1 = ifind( x ) ; int f2 = ifind( y ) ; if( f1 == f2 ) return false ; else if( f1 < f2 ) father[f1] = f2 ; else father[f2] = f1 ; return true ;}void kruskal(int n){ for(int i=0;i<n;i++) father[i]=i; sort(edge,edge+Count,cmp); for(int i=0;i<Count;i++){ if(Union(edge[i].s,edge[i].e)){ edge[i].visited=1; } }}double getlength( p a , p b ){ double len ; len = sqrt( double ( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) ) ) ; return len ;}int main(){ int T,S,Q; scanf("%d",&T); while(T--){ scanf("%d%d",&S,&Q); for(int i=0;i<Q;i++) scanf("%d%d",&point[i].x,&point[i].y); Count=0; for(int i=0;i<Q-1;i++) for(int j=i+1;j<Q;j++){ edge[Count].s=i; edge[Count].e=j; edge[Count].length=getlength(point[i],point[j]); edge[Count].visited=0; Count++; } kruskal(Q); int i; for(i=Count-1;i>=0;i--){ if(edge[i].visited){ S--; if(S==0) break; } } printf("%0.2f\n",edge[i].length); }}
0 0
- poj 2349(最小生成树)
- poj 2349 最小生成树
- poj 2349 最小生成树
- 【POJ 2349 最小生成树】
- poj 2349 最小生成树
- POJ 2349 Arctic Network [最小生成树]
- poj 2349 (最小生成树 Prim)
- poj 2349 最小生成树 careful everything!
- poj 2349 (最小生成树)Arctic Network
- POJ-2349 Arctic Network 最小生成树
- poj 2349(最小生成树应用)
- POJ 2349 Arctic Network 最小生成树
- poj-2349 Arctic Network 最小生成树
- poj 2349 Arctic Network 最小生成树
- poj 2349 Arctic Network 最小生成树
- poj 2349 Arctic Network 最小生成树~~
- Arctic Network (poj 2349 最小生成树)
- POJ 2349Arctic Network【最小生成树】
- STL之数值算法
- 2.容器
- 每个程序员都必须遵守的编程原则
- 使用VC++2013写出短小精悍的函数
- 对象作为数据成员
- 【POJ 2349 最小生成树】
- 【算法-查找之二】二分查找
- LigerUI 界面中,加上文本框限制输入长度功能!
- IP数据报的分片与重组分析
- 关于会议
- Hibernate框架学习(三) 关系映射
- vim 查看二进制文件
- MYSQL中 ENUM 类型
- 修改web office apps 默认在浏览器中打开