裸最小生成树。用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)的更多相关文章

  1. POJ-1751 Highways(最小生成树消边+输出边)

    http://poj.org/problem?id=1751 Description The island nation of Flatopia is perfectly flat. Unfortun ...

  2. POJ1751 Highways【最小生成树】

    题意: 给你N个城市的坐标,城市之间存在公路,但是由于其中一些道路损坏了,需要维修,维修的费用与公路长成正比(公路是直的). 但现有M条公路是完整的,不需要维修,下面有M行,表示不需要维修的道路两端的 ...

  3. POJ1751 Highways 2017-04-14 15:46 70人阅读 评论(0) 收藏

    Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14819   Accepted: 4278   Speci ...

  4. POJ1751 Highways(Prim)

    Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13182   Accepted: 3814   Speci ...

  5. POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)

                                                                                                         ...

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

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

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

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

  8. poj 2485 Highways 最小生成树

    点击打开链接 Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19004   Accepted: 8815 ...

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

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

  10. spoj 104 Highways (最小生成树计数)

    题目链接:http://www.spoj.pl/problems/HIGH/ 题意:求最小生成树个数. #include<algorithm> #include<cstdio> ...

随机推荐

  1. bug - colorWithPatternImage:

    // 在ios5之前, 再通过以下方法设置背景时, 有闪屏bug self.view.backgroundColor = [UIColor colorWithPatternImage:<#(no ...

  2. 关于\t

    tail -n 10000 sample_dbl.txt | python sitemap.py | sort -k 1 -t $'\t' | python sitecombiner.py > ...

  3. [转].NET程序在windows操作系统上独立运行的技术要点

    发现一个不错的网站,转载一篇文章方便查看 转自 http://www.linuxdot.net/bbsfile-3354   ===================================== ...

  4. JS数组整理

    1. 检测数组的方法: 1. instanceof[操作符]: var arr = []; console.log(arr instanceof Array);//true 1. instanceof ...

  5. hdu 4268

    set的利用: #include<cstdio> #include<set> #include<algorithm> #define maxn 100009 usi ...

  6. 数据库备份,远程执行SHELL脚本

    小小的东东,用于数据库的备份. 留存. #!/bin/sh keepDays= currentTime=`date "+%Y-%m-%d-%H-%M"` backPath=&quo ...

  7. ruby使用IO类读写文件

    path="test.txt" port=open(path) begin port.each_line{|line| p line.to_s } ensure port.clos ...

  8. [Hadoop源码解读](二)MapReduce篇之Mapper类

    前面在讲InputFormat的时候,讲到了Mapper类是如何利用RecordReader来读取InputSplit中的K-V对的. 这一篇里,开始对Mapper.class的子类进行解读. 先回忆 ...

  9. cogs_14_搭配飞行员_(二分图匹配+最大流,网络流24题#01)

    描述 http://cojs.tk/cogs/problem/problem.php?pid=14 有一些正飞行员和副飞行员,给出每个正飞行员可以和哪些副飞行员一起飞.一架飞机上必须一正一副,求最多多 ...

  10. NOI2014 随机数生成器

    随机数生成器 [问题描述] 小H最近在研究随机算法.随机算法往往需要通过调用随机数生成函数(例如Pascal中的random和C/C++中的rand)来获得随机性.事实上,随机数生成函数也并不是真正的 ...