Highways

POJ-1751

注意这里的样例答案也是对的,只是输出顺序改变,但是这也没关系,因为题目加了特殊判断。

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=755;
const int maxm=500004;
int n,m;
int e;
int x[maxn];
int y[maxn];
int set[maxn];
struct node{
int from;
int to;
double cost;
bool operator<(const node& t)const{
return cost<t.cost;
}
};
node edges[maxm];
int find(int x){
return x==set[x]?set[x]:set[x]=find(set[x]);
}
void kruskal(){
sort(edges,edges+e);
for(int i=0;i<e;i++){
int x=edges[i].from;
int y=edges[i].to;
double cost=edges[i].cost;
int from=find(x);
int to=find(y);
if(from!=to){
set[from]=to;
cout<<x<<" "<<y<<endl;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>n){
e=0;
for(int i=1;i<=n;i++){
cin>>x[i]>>y[i];
set[i]=i;
for(int j=1;j<i;j++){
double cost=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
edges[e++]=node({i,j,cost});
}
}
cin>>m;
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
int a1=find(a);
int b1=find(b);
if(a1!=b1)
set[a1]=b1;
}
kruskal();
}
return 0;
}

POJ-1751(kruskal算法)的更多相关文章

  1. POJ 1797 kruskal 算法

    题目链接:http://poj.org/problem?id=1797 开始题意理解错.不说题意了. 并不想做这个题,主要是想测试kruskal 模板和花式并查集的正确性. 已AC: /* 最小生成树 ...

  2. poj 2485 (kruskal算法)

    /*kruskal算法*/ #include <iostream> //#include <fstream> #include <algorithm> using ...

  3. ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

    题目链接:problemCode=1372">ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds     ...

  4. ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

    题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...

  5. poj 1789 Truck History(kruskal算法)

    主题链接:http://poj.org/problem?id=1789 思维:一个一个点,每两行之间不懂得字符个数就看做是权值.然后用kruskal算法计算出最小生成树 我写了两个代码一个是用优先队列 ...

  6. POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14021   Accepted: 5484   Specia ...

  7. POJ 1251 Jungle Roads - C语言 - Kruskal算法

    Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...

  8. POJ 1679 The Unique MST (次小生成树kruskal算法)

    The Unique MST 时间限制: 10 Sec  内存限制: 128 MB提交: 25  解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...

  9. POJ 1251 Jungle Roads(Kruskal算法求解MST)

    题目: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money w ...

  10. POJ 2421 Constructing Roads(Kruskal算法)

    题意:给出n个村庄之间的距离,再给出已经连通起来了的村庄.求把所有的村庄都连通要修路的长度的最小值. 思路:Kruskal算法 课本代码: //Kruskal算法 #include<iostre ...

随机推荐

  1. Array Transformer UVA - 12003

    题目:传送门 题意: 给你n个数,要进行m次操作 对于每次操作(l,r,v,p)代表:在区间[l,r]中有x(这个x是需要你自己找出来的)个数小于v,你需要把序列的第p个位置的值改成u∗k/(r−l ...

  2. Incorrect string value: '\xF0\x9F\x93\xB7</...' for column 'content' at row 1

    出现原因:当insert数据中有表情时发生.而这些表情是按照4个字节一个单位进行编码的,而我们使用的utf-8编码在mysql数据库中默认是按照3个字节一个单位进行编码的. 解决方法:将表字段字符集设 ...

  3. Mysql主从架构

    Mysql主从架构 1. 克隆虚拟机 克隆的虚拟机的网络适配,使得虚拟机可以进入局域网 vi /etc/sysconfig/network-scripts/ifcfg-eth0 删除 HWADDR所在 ...

  4. Redis 主从复制(Replication)

    为了保证服务的可用性,现代数据库都提供了复制功能,同时在多个进程中维护一致的数据状态. Redis 支持一主多从的复制架构,该功能被简化成了一条 SLAVEOF 命令,下面通过条命令来解析 Redis ...

  5. JAVA中高精度金额计算

    一般java代码中遇到高精度金额计算,日常使用bigDecimal类型. 在使用BigDecimal类来进行计算的时候,主要分为以下步骤: 1.用float或者double变量构建BigDecimal ...

  6. Keras模型拼装

    在训练较大网络时, 往往想加载预训练的模型, 但若想在网络结构上做些添补, 可能出现问题一二... 一下是添补的几种情形, 此处以单输出回归任务为例: # 添在末尾: base_model = Inc ...

  7. 前端知名人士 All In One

    前端知名人士 All In One 前端名人堂(中国) https://node.fequan.com/lecturer/ JavaScript的过去.现在和未来 1995年,Brendan Eich ...

  8. TensorFlow & Machine Learning

    TensorFlow & Machine Learning TensorFlow 实战 传统方式 规则 + 数据集 => 答案 无监督学习 机器学习 神经元网络 答案 + 数据集 =&g ...

  9. React Hooks: useRef All In One

    React Hooks: useRef All In One useRef https://reactjs.org/docs/hooks-reference.html#useref refs xgqf ...

  10. Xcode 切换 iOS 模拟器的 Dark 模式

    Xcode 切换 iOS 模拟器的 Dark 模式 SwiftUI // // ContentView.swift // MemorizeGame // // Created by 夏凌晨 on 20 ...