POJ 1751 Highways (kruskal)
题目链接: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)的更多相关文章
- POJ 1751 Highways (最小生成树)
Highways Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- POJ 1751 Highways 【最小生成树 Kruskal】
Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23070 Accepted: 6760 Speci ...
- POJ 1751 Highways (最小生成树)
Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...
- POJ 1751 Highways (ZOJ 2048 ) MST
http://poj.org/problem?id=1751 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2048 题目大 ...
- (poj) 1751 Highways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor ...
- POJ 1751 Highways(最小生成树&Prim)题解
思路: 一开始用Kruskal超时了,因为这是一个稠密图,边的数量最惨可能N^2,改用Prim. Prim是这样的,先选一个点(这里选1)作为集合A的起始元素,然后其他点为集合B的元素,我们要做的就是 ...
- POJ 1751 Highways(最小生成树Prim普里姆,输出边)
题目链接:点击打开链接 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has ...
- (最小生成树 Prim) Highways --POJ --1751
链接: http://poj.org/problem?id=1751 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1150 ...
- POJ 2485 Highways 最小生成树 (Kruskal)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
随机推荐
- linux/unix网络编程之epoll
转载自 Linux epoll模型 ,这篇文章讲的非常详细! 定义: epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显 ...
- VB获取浏览器版本
String userAgent; userAgent = Request.UserAgent; ) { // The browser is Microsoft Internet Explorer V ...
- UVa 1262 (第k字典序) Password
题意: 给出两个6行5列的字母矩阵,一个密码满足:密码的第i个字母在两个字母矩阵的第i列均出现. 然后找出字典序为k的密码,如果不存在输出NO 分析: 我们先统计分别在每一列均在两个矩阵出现的字母,然 ...
- Windows Live Writer测试插件
这是用WLW写的,测试 这是图片: 这是地图: 这是代码: 一会重发
- 【转】自定义iOS的Back按钮(backBarButtonItem)和pop交互手势(interactivepopgesturerecognizer) --- 不错
原文网址:http://blog.csdn.net/joonsheng/article/details/41362499 序 说到自定义UINavigetionController的返回按钮,iOS7 ...
- ubuntu1204上不能正常用emacs配合gocode进行自动补全
我按gocode的页面https://github.com/nsf/gocode上去做,可是还是未成功,,我确认auto-complete在c-mode中是可以使用的,因为有补全出来了, 我再找了ht ...
- Windows Phone 离主流系统还很远
调查机构 Kantar Worldpanel 在本月发布全球智能手机份额报告.报告显示,五月份除德国和澳大利亚出现下滑,Windows Phone 的市场份额在不少国家都实现增长. 英国,4.1% 升 ...
- JS 代码编一个倒时器
有时候在生活中,你需要一个JavaScript倒计时时钟,而不是一个末日装置设备.不管你是否有一次约会,销售.促销.或者游戏,你可以受益于使用原生JavaScript构建一个时钟,而不是拿到一个现成的 ...
- ISAPI在IIS7上的配置
主要介绍ISAPI的作用.ISAPI在IIS7上的配置.开发ISAPI的基本内容及使用VS 2008配置ISAPI DLL开发项目. 一.ISAPI介绍 缩写词=Internet Server App ...
- C++标准库开发心得
最近放弃MFC,改用C++标准库开发产品.毕竟MFC用熟了,马上改用STL还不太习惯.下面列出下总结的改用STL遇到的问题和解决办法: 1.清除空格 remove_if(iterBegin, iter ...