POJ
来源:互联网 发布:矩阵的雅可比矩阵 编辑:程序博客网 时间:2024/06/05 05:46
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns. However, there are still some towns that you can’t reach via a highway. It is necessary to build more highways so that it will be possible to drive between any pair of towns without leaving the highway system.
玄学,C++没过TLE了,G++过了。
用了kruskal。
把已连接的点先放到树里就可以了。
#include <stdio.h>#include <string.h>#include <iostream>#include<algorithm>#include <vector>#include <queue>#include <string>#include <math.h>#include <stdlib.h>using namespace std;#define INF 0x3f3f3f3f#define mem(arr,a) memset(arr,a,sizeof(arr))#define V 800#define LL long long int#define E 320000#define pow(a) ((a)*(a))struct edge{ LL u, v; LL w;};struct node{ int x, y;};node vet[V];edge es[E];bool cmp(edge a, edge b){ if (a.w!=b.w) return a.w < b.w; if (a.u != b.u) return a.u < b.u; return a.v < b.v;}int n, m;int par[V];int e = 0;int find(int x){ if (par[x] == x)return x; return par[x] = find(par[x]);}int main(){ cin >> n; for (int i = 1; i <= n; i++)par[i] = i; for (int i = 1; i <= n; i++){ scanf("%lld%lld", &vet[i].x, &vet[i].y); } for (int i = 1; i <= n; i++){ for (int j = 1; j < i; j++){ es[e].u = i, es[e].v = j, es[e++].w = pow(vet[i].x - vet[j].x) + pow(vet[i].y - vet[j].y); } } cin >> m; for (int i = 0; i < m; i++){ int a, b; scanf("%d%d", &a, &b); a = find(a); b = find(b); par[a] = b; } sort(es, es + e, cmp); for (int i = 0; i < e; i++){ int u = find(es[i].u); int v = find(es[i].v); if (u!=v){ par[u] = v; printf("%lld %lld\n", es[i].u, es[i].v); } }}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 文件上传与下载
- 如何自己开发一款js或者jquery插件
- 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]
- python入门(1)
- 构建拓扑
- POJ
- 文章标题
- java web学习总结14:JSP原理
- PHP获取二维数组中某一列的值集合
- 生活中常用的快捷键
- AngularJS访问服务器获取数据遍历生成表格,附Demo源码
- Maven学习总结(八)——使用Maven构建多模块项目
- 集群概念介绍
- Codeforces Round #432 (Div. 2)