poj1751
来源:互联网 发布:进销存数据库设计 编辑:程序博客网 时间:2024/05/01 14:16
/*本题就是最小生成树水题,但是一直tle,最后在poj看到有人说只有一组用例,试了试,直接ac,也是跪了。审题一定要认真(虽说是英文吧)*/#include <cstdio>#include <algorithm>#include <cmath>#define N 755using namespace std;typedef struct node {int x; int y;}point;typedef struct note {int u; int v; double w;}edge;point a[N];edge e[N*N/2];int n, m;int f[N];double dis(int i, int j){return sqrt(pow(a[i].x - a[j].x, 2) + pow(a[i].y - a[j].y, 2));}int cmp(edge a, edge b){return a.w < b.w;}void init(){for (int i = 1; i <= n; i++)f[i] = i;}int find(int x){return x == f[x] ? x : f[x] = find(f[x]);}int merge(int x, int y){int t1, t2;t1 = find(x); t2 = find(y);if (t1 != t2) { f[t2] = t1; return 1; }return 0;}int main(){scanf("%d", &n);int x, y;for (int i = 1; i <= n; i++)scanf("%d%d", &a[i].x, &a[i].y);m = 0;for (int i = 1; i <= n; i++){for (int j = i + 1; j <= n; j++){e[m].u = i;e[m].v = j;e[m++].w = dis(i, j);}}sort(e, e + m, cmp);init();int T, aa, bb;scanf("%d", &T);while (T--){scanf("%d%d", &aa, &bb);merge(aa, bb);}int cnt = 0;point path[N];for (int i = 0; i < m; i++){if (merge(e[i].u, e[i].v)){path[cnt].x = e[i].u;path[cnt].y = e[i].v;cnt++;}if (cnt >= n - 1)break;//已找到最小生成树}for (int i = 0; i < cnt; i++)printf("%d %d\n", path[i].x, path[i].y);return 0;}
0 0
- poj1751
- poj1751 Highways
- POJ1751 Highways
- POJ1751 Highways
- poj1751 highway
- POJ1751 Highways
- poj1751 Highways
- poj1751——Highways
- poj1751-最小生成树
- POJ1751 Highways(kru)
- POJ1751->最小生成树
- poj1751 Highway (Prim)
- Prim POJ1258 POJ2485 POJ1789 POJ1751
- poj1751 Highways(最小生成树)
- POJ1751 PRIM基础题(模板)
- poj1751 Highways 最小生成树(小组赛A)
- poj1751 highways 最小生成树记录路径
- wust April Chanllenge 2014 C题 poj1751 Highways
- Analysis of 【Dropout】
- java虚拟机
- 【mapreduce】hadoop2.x—mapreduce实战和总结
- Java高并发,如何解决,什么方式解决
- 使用swoole实现生产者消费者模型(2)
- poj1751
- Android ADB命令的使用
- HDU1010 Tempter of the Bone (DFS & 奇偶剪枝)
- 【PAT】1096. Consecutive Factors
- android studio 自定义皮肤主题 ColorThemes
- MySQL中select * for update锁表的问题
- Android卸载测试应用后再次安装应用失败
- 软件管理 设备访问(初稿)
- 学习Cortex-M:结构体 vs 分散变量