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. 洛谷 P2391.白雪皑皑 (并查集,思维)

    题意:有\(n\)个点,对这些点进行\(m\)次染色,第\(i\)次染色会把区间\((i*p+q)\ mod\ N+1\)和\((i*q+p)\ mod\ N+1\)之间的点染成颜色\(i\),问最后 ...

  2. C# 网络流

    流(stream)是对串行传输的数据的一种抽象表示,底层的设备可以是文件.外部设备.主存.网络套接字等等. 流有三种基本的操作:写入.读取和查找. 如果数据从内存缓冲区传输到外部源,这样的流叫作&qu ...

  3. Zabbix 微信监控报警

    Zabbix-Server 设置 # 使脚本目录生效 [root@zabbix ~]# grep 'script' /etc/zabbix/zabbix_server.conf # AlertScri ...

  4. C# 类 (5)

    Static 静态成员 创建一个类,实例化它,然后使用实例的各种方法或者属性 然而有时候我们不想实例化这个类,难道我们就没法用他里面的成员了吗?比如计算矩形的面积,就是长*宽,万年不变 当然可以,用s ...

  5. Pycharm+任务栏悬浮+docked mode

    先点下所想改变模式的模块, 然后: Window -> Activate tool window -> docked mode.

  6. Loss_Function_of_Linear_Classifier_and_Optimization

    Loss_Function_of_Linear_Classifier_and_Optimization Multiclass SVM Loss:    Given an example(xi, yi& ...

  7. 微信公众号代码高亮美化工具 All In One

    微信公众号代码高亮美化工具 All In One markdown 美化 mdnice 编辑器 https://www.mdnice.com/ https://github.com/mdnice ma ...

  8. iOS WebView All In One

    iOS WebView All In One WKWebView / UIWebView Swift Playground //: A UIKit based Playground for prese ...

  9. self-publishing ebook guide all in one

    self-publishing ebook guide all in one 自助出版电子书指南 第1课:让我们自己出版一本书! 与您会想到的很多人相反,自我出版并不容易. 您不仅必须写书,而且还必须 ...

  10. Flutter in DartPad

    Flutter in DartPad Gist Sharing https://github.com/dart-lang/dart-pad/wiki/Sharing-Guide https://gis ...