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> ...
随机推荐
- struts2用了哪几种模式
代理模式 责任连模式 ActionVacation 迭代模式
- chrome浏览器扩展--QQ群查看器(1)
QQ群查看器--chrome浏览器扩展 源码及程序下载地址:http://pan.baidu.com/share/link?shareid=3636190804&uk=1678089569 关 ...
- WiFi与WLAN的区别
很多人到了某一个地方首选要找的就是无线网络,有时候还会问周围的人:这里有WiFi吗?或者说:这里有WLAN吗?那WiFi和WLAN有什么区别呢? 简单来讲,WiFi是无线保真(wireless fid ...
- Tomacat服务器的安装和配置
一, Tomcat服务器的下载地址(Apache Tomcat的官网): http://tomcat.apache.org/download-70.cgi 这里为了稳定性安装的版本为7.0. 截止目 ...
- Codeforces Round #239 (Div. 2) C. Triangle
time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputoutput:standard o ...
- HDU 2369 Broken Keyboard(字符串)
点我看题目 题意 : 这个人的键盘坏了,最多只能按n个键,给你一串字符串,问你找一个最长的字串,这个字串中包含的不同的字母不能超过n个. 思路 : 比赛的时候脑子没转过来,一直没模拟出来,都不知道怎么 ...
- http://blog.csdn.net/shirdrn/article/details/6270506
http://blog.csdn.net/shirdrn/article/details/6270506
- c#中的delegate(委托)和event(事件)
c#中的delegate(委托)和event(事件) 一.delegate是什么东西? 完全可以把delegate理解成C中的函数指针,它允许你传递一个类A的方法m给另一个类B的对象,使得类B的对象能 ...
- easyui源码翻译1.32--Calendar(日历)
前言 前几天加班比较忙 未能及时更新翻译的 今天多发布几篇..下载该插件翻译源码 日历控件显示一个月的日历,允许用户选择日期和移动到下一个或上一个月.默认情况下,一周的第一天是周日.它可以通过设置'f ...
- 测试ODBC与OLE
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data. ...