POJ1751--Highways(最小生成树,kauskal)
裸最小生成树。用kauskal做方便一些。
不得不说这么大数据用cin cout 真是作死。。活该T那么多次。。。
/*************************************************
Problem: 1751 User: G_lory
Memory: 4640K Time: 594MS
Language: C++ Result: Accepted
*************************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring> using namespace std; const int MAXN = 755;
const int MAXM = MAXN * MAXN; int par[MAXN];
int rk[MAXN]; struct Edge
{
int u, v;
double w;
bool operator<(const Edge a) const
{
return w < a.w;
}
} edge[MAXM]; int tol; void addedge(int u, int v, double w)
{
edge[tol].u = u;
edge[tol].v = v;
edge[tol++].w = w;
} void init(int n)
{
for (int i = 0; i <= n; ++i)
{
rk[i] = 0;
par[i] = i;
}
} int find(int x)
{
if (par[x] == x) return x;
return par[x] = find(par[x]);
} void unite(int x, int y)
{
x = find(x);
y = find(y);
if (x == y) return ;
if (rk[x] < rk[y]) par[x] = y;
else par[y] = x;
if (rk[x] == rk[y]) rk[x]++;
} void Kruskal(int n, int m)
{
sort(edge, edge + tol);
int cnt = 0;
for (int i = 0; i < tol; i++)
{
int t1 = find(edge[i].u);
int t2 = find(edge[i].v);
if (t1 != t2)
{
cout << edge[i].u << " " << edge[i].v << endl;
unite(t1, t2);
cnt++;
}
if (cnt == n - 1 - m) break;
}
} struct Point {
double x, y;
double dis(Point a)
{
return sqrt( (x - a.x) * (x - a.x) + (y - a.y) * (y - a.y) );
}
} p[MAXN]; int main()
{
int n, m;
scanf("%d", &n);
tol = 0;
for (int i = 1; i <= n; ++i)
scanf("%lf%lf", &p[i].x, &p[i].y);
for (int i = 1; i <= n; ++i)
for (int j = i + 1; j <= n; ++j)
addedge(i, j, p[i].dis(p[j]));
init(n);
scanf("%d", &m);
int a, b;
int p = 0;
for (int i = 0; i < m; ++i)
{
scanf("%d%d", &a, &b);
if (find(a) != find(b))
{
unite(a, b);
++p;
}
}
Kruskal(n, p);
return 0;
}
POJ1751--Highways(最小生成树,kauskal)的更多相关文章
- POJ-1751 Highways(最小生成树消边+输出边)
http://poj.org/problem?id=1751 Description The island nation of Flatopia is perfectly flat. Unfortun ...
- POJ1751 Highways【最小生成树】
题意: 给你N个城市的坐标,城市之间存在公路,但是由于其中一些道路损坏了,需要维修,维修的费用与公路长成正比(公路是直的). 但现有M条公路是完整的,不需要维修,下面有M行,表示不需要维修的道路两端的 ...
- POJ1751 Highways 2017-04-14 15:46 70人阅读 评论(0) 收藏
Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14819 Accepted: 4278 Speci ...
- POJ1751 Highways(Prim)
Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13182 Accepted: 3814 Speci ...
- POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)
...
- POJ 2485 Highways 最小生成树 (Kruskal)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- POJ 1751 Highways(最小生成树Prim普里姆,输出边)
题目链接:点击打开链接 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has ...
- poj 2485 Highways 最小生成树
点击打开链接 Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19004 Accepted: 8815 ...
- POJ 1751 Highways (最小生成树)
Highways Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- spoj 104 Highways (最小生成树计数)
题目链接:http://www.spoj.pl/problems/HIGH/ 题意:求最小生成树个数. #include<algorithm> #include<cstdio> ...
随机推荐
- Oracle索引扫描
Oracle索引扫描:先通过index查找到索引的值,并根据索引的值对应的rowid值(对于非唯一索引可能返回多个rowid值)直接从表中得到具体的数据.一个rowid唯一的表示一行数据,该行对应的数 ...
- Python之创建tuple
tuple是另一种有序的列表,中文翻译为" 元组 ".tuple 和 list 非常类似,但是,tuple一旦创建完毕,就不能修改了. 同样是表示班里同学的名称,用tuple表示如 ...
- <django中render_to_response的可选参数和使用方法>
在django官方文档中有比较详细的介绍,在此我按照自己的理解适当的阐述一下: return render_to_response(①'my_template.html', ②my_data_dict ...
- POJ 1236 Network of Schools[连通分量]
题目链接:http://poj.org/problem?id=1236题目大意:给出N台电脑,电脑间单向连通传送文件问题1.网络中最少放几个文件保证所有电脑都能接受到文件问题2.最少向网络中加几条线保 ...
- Struts2 interceptor使用经验小结
1. interceptor 调用Spring容器中的bean 在interceptor中常有需要调用Spring Bean的需要,其实很简单和Struts2的Action一样配置即可. Spring ...
- Codeforces Round #215 (Div. 1)
A Sereja and Algorithm 题意:给定有x,y,z组成的字符串,每次询问某一段s[l, r]能否变成变成zyxzyx的循环体. 分析: 分析每一段x,y,z数目是否满足构成循环体,当 ...
- RUST叫系统编程语言,而GO是网络编程语言
用GO你必须要搞清楚一点,GO不是面向CPU密集计算的,而是面向IO密集计算的!!!RUST才是面向CPU密集计算的语言,所以RUST叫系统编程语言,而GO是网络编程语言.GO跟JAVA的Spring ...
- int指令理解
以下是王爽老师的<汇编语言>中第十五章中的一段程序代码,其功能是增加9号中断的功能,当按下Esc键时屏幕中显示的字母改变颜色 assume cs:codesg,ss:stack,ds:da ...
- Form.KeyPreview 属性
Form.KeyPreview 属性 今天再做KeyDown 和 KeyUp 事件时,就是忘了设置,窗体的KeyPreview 属性,所以KeyDown 和 KeyUp 事件没有反应(这里说明一下,本 ...
- MPlayer
名称 mplayer − 电影播放器 mencoder − 电影编解码器 概要 mplayer [选项] [文件|URL|播放列表|−] mplayer [选项] 文件1 [指定选项] [文件 ...