题目链接:http://poj.org/problem?id=1751

题意是给你n个点的坐标,然后给你m对点是已经相连的,问你还需要连接哪几对点,使这个图为最小生成树。

这里用kruskal不会超时,用prim应该会超时,特别注意在输入的时候不要多组输入,否则会TLE。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = ;
struct edge {
int u , v , cost;
}a[MAXN * MAXN];
int x[MAXN] , y[MAXN] , cont , par[MAXN] , cnt , f , ans[MAXN * ];
bool vis[MAXN][MAXN]; void init(int n) {
for(int i = ; i <= n ; i++) {
par[i] = i;
}
memset(vis , false , sizeof(vis));
cont = ;
cnt = n;
} int Find(int n) {
if(n == par[n])
return n;
return (par[n] = Find(par[n]));
} bool cmp(edge a , edge b) {
return a.cost < b.cost;
} int kruskal() {
for(int i = ; i <= cont ; i++) {
if(cnt == )
break;
int u = Find(a[i].u) , v = Find(a[i].v);
if(u != v) {
ans[f++] = a[i].u;
ans[f++] = a[i].v;
par[u] = v;
cnt--;
}
}
} int main()
{
int n , m , u , v;
scanf("%d" , &n);
{
init(n);
for(int i = ; i <= n ; i++) {
scanf("%d %d" , x + i , y + i);
}
scanf("%d" , &m);
while(m--) {
scanf("%d %d" , &u , &v);
u = Find(u) , v = Find(v);
vis[u][v] = vis[v][u] = true;
if(u != v) {
par[u] = v;
cnt--;
}
}
for(int i = ; i <= n ; i++) {
for(int j = ; j < i ; j++) {
if(vis[i][j])
continue;
cont++;
a[cont].u = j , a[cont].v = i;
a[cont].cost = (x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]);
}
}
sort(a + , a + cont + , cmp);
f = ;
kruskal();
for(int i = ; i < f ; i += ) {
printf("%d %d\n" , ans[i] , ans[i + ]);
}
}
}

POJ 1751 Highways (kruskal)的更多相关文章

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

    Highways Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  2. POJ 1751 Highways 【最小生成树 Kruskal】

    Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23070   Accepted: 6760   Speci ...

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

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

  4. POJ 1751 Highways (ZOJ 2048 ) MST

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

  5. (poj) 1751 Highways

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor ...

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

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

  7. POJ 1751 Highways(最小生成树Prim普里姆,输出边)

    题目链接:点击打开链接 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has ...

  8. (最小生成树 Prim) Highways --POJ --1751

    链接: http://poj.org/problem?id=1751 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1150 ...

  9. POJ 2485 Highways 最小生成树 (Kruskal)

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

随机推荐

  1. gsp序列模式挖掘

    数据挖掘进阶之序列模式挖掘GSP算法 绪 继续数据挖掘方面算法的讲解,前面讲解了数据挖掘中关联规则算法FP-Growth的实现.此篇博文主要讲解基于有趣性度量标准的GSP序列模式挖掘算法.有关论文后期 ...

  2. UVa 10780 (质因数分解) Again Prime? No Time.

    求mk整除n!,求k的最大值. 现将m分解质因数,比如对于素数p1分解出来的指数为k1,那么n!中能分解出多少个p1出来呢? 考虑10!中2的个数c:1~10中有10/2个数是2的倍数,c += 5: ...

  3. ubuntu下安装使用vmware、kvm、xen

    一. 概念介绍: (1)全虚拟化(Full Virtulization) 简介:主要是在客户操作系统和硬件之间捕捉和处理那些对虚拟化敏感的特权指令,使客户操作系统无需修改就能运行, 速度会根据不同的实 ...

  4. Java [Leetcode 328]Odd Even Linked List

    题目描述: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...

  5. 【ASP.NET MVC】"[A]System.Web.WebPages.Razor.Configuration.HostSection 无法强制转换为 ..."的解决办法

    1.错误页面: “/”应用程序中的服务器错误. [A]System.Web.WebPages.Razor.Configuration.HostSection 无法强制转换为 [B]System.Web ...

  6. Stamps and Envelope Size

    题意: 容量为s的信封,给n组邮票的面值,求哪一组能组成的连续的面值的最大值最大,若有多组答案,输出面值数量最小的一组,若数量相等,输出最大面值最小的一组,若最大面值相等,输出第二大面值最小的一组,依 ...

  7. MockMvc和Mockito之酷炫使用

    由于项目中需要添加单元测试,所以查询之后发现Mockito非常适合现在的web项目. 首先需要添加pom依赖: <dependency> <groupId>junit</ ...

  8. 为duilib的MenuDemo增加消息响应,优化代码和显示效果

    转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/38253297 第一部分 我在前一段时间研究了怎么制作duilib的菜单, ...

  9. C#冒泡排序详解

    今天写一简单的冒泡排序,带有详细的中文注释,新手一定要看看! 因为这是找工作面试时经常 笔试 要考的题目. using System; using System.Collections.Generic ...

  10. 如何在 Windows Azure 的虚拟机 ubuntu 上面安装和配置 openVPN(二)

    第二步:登录到虚拟机 一旦创建好虚拟机后,默认azure会打开TCP 22端口,即SSH的端口.所以,我们可以通过远程连接,访问和管理该虚拟机. 首先,下载一个PuTTY软件.该软件很简单,就一个可执 ...