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. Android中的MVP架构初探

    说来羞愧,MVP的架构模式已经在Android领域出现一两年了.可是到今天自己才開始Android领域中的MVP架构征程. 闲话不多说,開始吧. 一.架构演变概述 我记得我找第一份工作时,面试官问我& ...

  2. HDOJ 5399 Too Simple

    每个函数都必须是一个排列,经过连续的一段确定函数后数字不能少. 满足上面的条件的话,仅仅要有一个-1函数特别的排列一下就能够满足要求,剩下的能够任意填 没有-1的话特判 Too Simple Time ...

  3. 小胖说事29-----iOS中Navigation中左滑pop页面的三种方法

    第三中类型.自己定义任何位置返回页面的方式,上边的类就是.m,大家能够贴过去使用.这个类是继承NavigationController的.用这个类初始化rootController就能够了.这里还有源 ...

  4. [NOI.AC#30]candy 贪心

    链接 一个直观的想法是,枚举最小的是谁,然后二分找到另外一个序列对应位置更新答案,复杂度 \(O(NlogN)\) 实际上不需要二分,因为每次当最大的变大之后,原来不行的最小值现在也一定不行,指针移动 ...

  5. android notify

    notify http://examples.javacodegeeks.com/android/core/ui/notifications/android-notifications-example ...

  6. Vue前后端分离常用JS函数点(一)

    1.筛选过滤:array.filter(function(val){});  会把array中符合规则的数组元素按array里面的元素顺序保留下来. // 官方解释:按返回true或者false,把不 ...

  7. CISP/CISA 每日一题 二

    CISA 观察和测试用户操作程序 1.职责分离:确保没人具有执行多于一个下列处理过程的能力:启动.授权.验证或分发 2.输入授权:可以通过在输入文件上的书面授权或唯一口令的使用来获得证据 3.平衡:验 ...

  8. mysql新加入用户与删除用户详细操作命令

    方法1 :使用mysql root(root权限)用户登陆直接赋权也能够创建用户 /usr/bin/mysqladmin -u root password 123456 mysql -uroot -p ...

  9. 109.vprintf vfprintf vscanf vfscanf

    vprintf //输出到屏幕 int POUT(char *str, ...) { va_list arg_p=NULL; //读取 va_start(arg_p, str); //接受可变参数 i ...

  10. 1.1 Introduction中 Guarantees官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Guarantees Kafka的保证(Guarantees) At a high- ...