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;
}

POJ 1751 Highways (ZOJ 2048 ) MST的更多相关文章

  1. POJ 1751 Highways(最小生成树&Prim)题解

    思路: 一开始用Kruskal超时了,因为这是一个稠密图,边的数量最惨可能N^2,改用Prim. Prim是这样的,先选一个点(这里选1)作为集合A的起始元素,然后其他点为集合B的元素,我们要做的就是 ...

  2. POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配

    两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...

  3. POJ 1751 Highways (最小生成树)

    Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...

  4. POJ 2590 Steps (ZOJ 1871)

    http://poj.org/problem?id=2590 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1871 题目大 ...

  5. POJ 3279 Fliptile(翻格子)

    POJ 3279 Fliptile(翻格子) Time Limit: 2000MS    Memory Limit: 65536K Description - 题目描述 Farmer John kno ...

  6. POJ - 3308 Paratroopers(最大流)

    1.这道题学了个单词,product 还有 乘积 的意思.. 题意就是在一个 m*n的矩阵中,放入L个敌军的伞兵,而我军要在伞兵落地的瞬间将其消灭.现在我军用一种激光枪组建一个防御系统,这种枪可以安装 ...

  7. 转载:poj题目分类(侵删)

    转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码)  ...

  8. POJ 3259 Wormholes (Bellman_ford算法)

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  9. POJ 1006 Biorhythms (中国剩余定理)

    在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...

随机推荐

  1. Javascript:存储和读取cookie

    Cookie是网页开发中的一项重要技术,用于在本地存储一些信息(如username,password.登录状态)以便用户下一次訪问时使用(或在其他页面使用). cookie的格式是键值对,多个键值对之 ...

  2. 多类别分类问题由 confusion matrix 到分类准确率(accuracy)的计算

    conf_mat = confusionmat(y_true, y_pred); % 首先根据数据集上的真实 label 值,和训练算法给出的预测 label 值, % 计算 confusion ma ...

  3. c++运算符重载笔记

    运算符重载的概念:给原有的运算符赋予新的功能: 比如:+ 不仅可以做算术运算也可以连接俩个字符串 一元运算符:只与一个操作数进行运算 比如 正负号 运算符重载的本质是:函数重载. <<与& ...

  4. Zabbix监控告警

    一 钉钉告警 1.1.1 添加钉钉机器人 发起群聊 创建完群聊选择,机器人管理 选择你要绑定的群聊 复制下面地址留用 1.1.2 编写钉钉告警脚本 安装requests库,HTTP客户端, # yum ...

  5. SpringMVC,Mybatis,FreeMarker连接mycat示例(一)

    首页 > 程序开发 > 软件开发 > Java > 正文 SpringMVC,Mybatis,FreeMarker连接mycat示例(一) 项目结构如图: 首先是各种配置文件, ...

  6. 决策树2 -- CART算法

    声明: 1,本篇为个人对<2012.李航.统计学习方法.pdf>的学习总结.不得用作商用,欢迎转载,但请注明出处(即:本帖地址). 2,因为本人在学习初始时有非常多数学知识都已忘记.所以为 ...

  7. 初识ThreadLocal

    近期公司在进行Java开发者的招聘活动,当中有一道面试题是这种:"请简单描写叙述一下ThreadLocal类的作用." 结果发现有非常多的面试者没有听说过ThreadLocal或者 ...

  8. 18. springboot整合jsp

    转自:https://blog.csdn.net/u012562943/article/details/51836729

  9. 100.dll调用

    在dll中声明 _declspec(dllexport) ; _declspec(dllexport)void go() { MessageBoxA(, ); } 调用dll HINSTANCE hl ...

  10. golang recover panic 流程控制的可达与不可达

    --------------------------流程控制可达----------------------------- package main import "fmt" fu ...