POJ 1751 Highways (ZOJ 2048 ) MST
来源:互联网 发布:小提琴谱软件 编辑:程序博客网 时间:2024/05/19 04:28
http://poj.org/problem?id=1751
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2048
题目大意:
给你n个点的坐标,并且m个点之间已经建立起连接,让你输出剩下的MST的连接点。
两题其实一样。就输入一点点不同。
因为题目采用Special Judge故水题一题。
POJ:
#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int MAXN=755;const int INF=9999999;int fa[MAXN];struct point{double x,y;}ship[MAXN];struct dot{int x,y;double dis;}dis[MAXN*MAXN];int find(int cur){return fa[cur]==cur?cur:fa[cur]=find(fa[cur]);}bool operator < (const dot &a,const dot& b){return a.dis<b.dis;}int main(){int n,kase=1;while(~scanf("%d",&n)){if(kase!=1)printf("\n");kase++;int i,j;for(i=1;i<=n;i++)scanf("%lf%lf",&ship[i].x,&ship[i].y);int len=0;for(i=1;i<=n;i++){for(j=i+1;j<=n;j++){dis[len].x=i;dis[len].y=j;dis[len].dis=sqrt((ship[j].y -ship[i].y) *(ship[j].y -ship[i].y) + (ship[j].x -ship[i].x)*(ship[j].x -ship[i].x));len++;}}for(i=1;i<=n;i++)fa[i]=i;int m;scanf("%d",&m);for(i=1;i<=m;i++){int x,y;scanf("%d%d",&x,&y);int rootx=find(x);int rooty=find(y);fa[rootx]=rooty;}sort(dis,dis+len);for(i=0;i<len;i++) {int rootx=find(dis[i].x);int rooty=find(dis[i].y);if(rootx!=rooty){printf("%d %d\n",dis[i].x,dis[i].y);fa[rootx]=rooty;}}}return 0;}
ZOJ:
#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int MAXN=755;const int INF=9999999;int fa[MAXN];struct point{double x,y;}ship[MAXN];struct dot{int x,y;double dis;}dis[MAXN*MAXN];int find(int cur){return fa[cur]==cur?cur:fa[cur]=find(fa[cur]);}bool operator < (const dot &a,const dot& b){return a.dis<b.dis;}int main(){int T,n,kase=1;scanf("%d",&T);while(T--){if(kase!=1)printf("\n");kase++;scanf("%d",&n);int i,j;for(i=1;i<=n;i++)scanf("%lf%lf",&ship[i].x,&ship[i].y);int len=0;for(i=1;i<=n;i++){for(j=i+1;j<=n;j++){dis[len].x=i;dis[len].y=j;dis[len].dis=sqrt((ship[j].y -ship[i].y) *(ship[j].y -ship[i].y) + (ship[j].x -ship[i].x)*(ship[j].x -ship[i].x));len++;}}for(i=1;i<=n;i++)fa[i]=i;int m;scanf("%d",&m);for(i=1;i<=m;i++){int x,y;scanf("%d%d",&x,&y);int rootx=find(x);int rooty=find(y);fa[rootx]=rooty;}sort(dis,dis+len);for(i=0;i<len;i++) {int rootx=find(dis[i].x);int rooty=find(dis[i].y);if(rootx!=rooty){printf("%d %d\n",dis[i].x,dis[i].y);fa[rootx]=rooty;}}}return 0;}
0 0
- POJ 1751 Highways (ZOJ 2048 ) MST
- poj 1751 Highways MST
- zoj 2048 Highways poj 1751
- zoj 2048 && poj 1751 Highways
- ZOJ 2048 POJ 1751 Highways
- POJ 1751 Highways(MST 算法)
- poj 2485 Highways(MST)
- POJ 2485 Highways (MST.Prim)
- POJ 2485 Highways ( MST 水题 )
- ZOJ 2048 / POJ 2485 Highways ( kruskal )
- poj 1751 Highways(Kruskal)
- POJ 1751 Highways (kruskal)
- zoj 2048 Highways(最小生成树)
- zoj 2048 Highways(Kruskal~)
- ZOJ 2048 Highways【kruskal】
- zoj - 2048 - Highways
- zoj 2048 Highways
- ZOJ 2048 highways
- uikit 类图
- 进程控制理论<五>--进程通信方式对比
- 线程
- Java的不定长度参数
- Spring mvc 注释 + ibatis 整合
- POJ 1751 Highways (ZOJ 2048 ) MST
- QTabWidget实现类似QQ聊天窗口
- Python的包管理工具Pip
- android listView 点击无响应的解决方法
- DNP3执行漏洞
- 1999~2012年全国百优博士论文 (计算机类)
- 就从这出发吧
- 菜单问题
- VMware虚拟机磁盘压缩