POJ 1751 Highways (ZOJ 2048 ) MST
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的更多相关文章
- POJ 1751 Highways(最小生成树&Prim)题解
思路: 一开始用Kruskal超时了,因为这是一个稠密图,边的数量最惨可能N^2,改用Prim. Prim是这样的,先选一个点(这里选1)作为集合A的起始元素,然后其他点为集合B的元素,我们要做的就是 ...
- POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配
两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...
- POJ 1751 Highways (最小生成树)
Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...
- POJ 2590 Steps (ZOJ 1871)
http://poj.org/problem?id=2590 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1871 题目大 ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- POJ - 3308 Paratroopers(最大流)
1.这道题学了个单词,product 还有 乘积 的意思.. 题意就是在一个 m*n的矩阵中,放入L个敌军的伞兵,而我军要在伞兵落地的瞬间将其消灭.现在我军用一种激光枪组建一个防御系统,这种枪可以安装 ...
- 转载:poj题目分类(侵删)
转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ 1006 Biorhythms (中国剩余定理)
在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...
随机推荐
- python版 百度签到
经常玩贴吧,刚学python ,所以自己弄了一个python版的签到程序.自己的东西总是最好的. 登陆模块参考的http://www.crifan.com/emulate_login_website_ ...
- Android ImageView设置图片原理(上)
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 首先关于图片加载到ImageView上,我们来讨论几个问题: 如下: imageView.setIm ...
- try {}里有一个return语句 finally执行顺序
先看例子 package example; class Demo{ public static void main(String args[]) { int x=1; System.out.print ...
- npm install (让别人下载自己的包)
好几天没更新了,再这里跟大家说声抱歉,今天来点干货. 发布一个包在npm上,可以供世界所有人使用,想一下,以前我们做项目,都是在npm install 别人的包,什么时候才能install我们自己的包 ...
- go reflect 调用方法
package main import ( "fmt" "reflect" ) type A struct { } func (A) Test() { fmt. ...
- golang webservice[ json Martini webframe]
golang webservice[ json Martini webframe] https://github.com/brunoga/go-webservice-sample 自己修改了一下例子, ...
- NVM安装nodejs的方法
安装nodejs方式有很多种. 第一种:官网下载 通过nodejs官网下载安装 ,但有个缺陷,不同版本的nodejs无法顺利的切换. 第二种: NVM安装 NVM可以帮助我们快速切换 node版本 ...
- COGS——T 2057. [ZLXOI2015]殉国
http://cogs.pro/cogs/problem/problem.php?pid=2057 ★☆ 输入文件:BlackHawk.in 输出文件:BlackHawk.out 评测插件 ...
- APM2.8 Rover 自己主动巡航车设计(固件安装和设置)
1.2 APM2.8软件安装与固件下载 下载Mission Planner这个地面基站软件,这里介绍的是windoews平台下的,在MAC或者linux下能够使用QGroundCont基于QT编写的地 ...
- UML学习之初步总结
UML(Unified Modeling Language)即统一建模语言,是一种开放的方法,用于说明.可视化.构建和编写一个正在开发的.面向对象的.软件密集系统的制品的开放方法.UML展现了一系列最 ...