题意:平面上n个点修路,已经修好了m条,再修若干条使得点之间连通,求最小代价的方案。

思路:基本上是裸的最小生成树了,我这里存边直接存在multyset了,取的时候也比较方便,我本来就是这么考虑的,队友打了一发朴素的排序的超时了。

 #include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define MAXN 755
using namespace std;
struct Edge{
int from, to;
LL dis;
Edge(int from, int to, LL dis):from(from), to(to), dis(dis){};
bool operator <(const Edge &b) const{
return dis < b.dis;
}
};
multiset<Edge> s;
struct Point{
int x, y;
}p[MAXN];
LL dis[MAXN][MAXN];
bool vis[MAXN][MAXN];
int father[MAXN];
int scan(){
int res = , ch, flag = ; if((ch = getchar()) == '-')
flag = ; else if(ch >= '' && ch <= '')
res = ch - '';
while((ch = getchar()) >= '' && ch <= '' )
res = res * + ch - ''; return flag ? -res : res;
}
LL getdis(Point a, Point b){
LL x = abs(a.x - b.x);
LL y = abs(a.y - b.y);
return x * x + y * y;
}
int find(int x){
if(father[x] == x) return x;
father[x] = find(father[x]);
return father[x];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // OPEN_FILE
int n = scan();
for(int i = ; i <= n; i++){
father[i] = i;
p[i].x = scan();
p[i].y = scan();
}
s.clear();
LL dis;
for(int i = ; i <= n; i++){
for(int j = i + ; j <= n; j++){
dis = getdis(p[i] ,p[j]);
s.insert(Edge(i, j, dis));
}
}
int m =scan();
int x, y;
for(int i = ; i <= m; i++){
x = scan();
y = scan();
vis[x][y] = true;
x = find(x);
y = find(y);
if(x == y) continue;
father[x] = y;
}
multiset<Edge>::iterator it = s.begin();
while(it != s.end()){
Edge u = *it;
it++;
if(vis[u.from][u.to] || vis[u.to][u.from]) continue;
x = find(u.from);
y = find(u.to);
if(x == y) continue;
father[x] = y;
printf("%d %d\n", u.from, u.to);
} }

Gym - 100203H Highways 最小生成树的更多相关文章

  1. Codeforces Gym 100203H Highways 最小生成树

    原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 给你平面上若干点,生成一颗完全图,让 ...

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

                                                                                                         ...

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

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

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

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

  5. poj 2485 Highways 最小生成树

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

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

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

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

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

  8. POJ2485 Highways(最小生成树)

    题目链接. 分析: 比POJ2253要简单些. AC代码: #include <iostream> #include <cstdio> #include <cstring ...

  9. 最小生成树练习3(普里姆算法Prim)

    风萧萧兮易水寒,壮士要去敲代码.本女子开学后再敲了.. poj1258 Agri-Net(最小生成树)水题. #include<cstdio> #include<cstring> ...

随机推荐

  1. LVM 镜像硬盘更换、数据恢复(centos7.4 redhat7.5)

      案例说明 Centos7 VG:vg LV:vg-lvRedhat 7.5VG:vgtest LV:lvtest 目的:模拟硬盘 /dev/sdb损坏.在线添加新硬盘/dev/sdc,lv镜像数据 ...

  2. 使用Jmeter工具对http接口进行压力测试

    1.访问apache官网下载Jmeter工具 地址:https://jmeter.apache.org/download_jmeter.cgi 2.解压压缩包后运行bin目录下jmeter.bat启动 ...

  3. Swagger 生成 PHP API 接口文档

    Swagger 生成 PHP API 接口文档 Lumen微服务生成Swagger文档 1.概况 有同学反馈写几十个接口文档需要两天的工作量, 随着多部门之间的协作越来越频繁, 维护成本越来越高, 文 ...

  4. Linux 设备驱动之 UIO 机制(基本概念)

    一个设备驱动的主要任务有两个: 1. 存取设备的内存 2. 处理设备产生的中断 对于第一个任务.UIO 核心实现了mmap()能够处理物理内存(physical memory),逻辑内存(logica ...

  5. 使用Openfire和Asmack实现IM功能,常常出现“Thread already started”的错误

    近期使用Openfire和Asmack实现Android端的IM功能,可是实际使用的过程中,常常出现"java.lang.IllegalThreadStateException:Thread ...

  6. Java单例你所不知道的事,与Volatile关键字有染

    版权声明:本文为博主原创文章,未经博主允许不得转载. 如果问一个码农最先接触到的设计模式是什么,单例设计模式一定最差也是“之一”. 单例,Singleton,保证内存中只有一份实例对象存在. 问:为什 ...

  7. form表单系列中文件上传及预览

    文件上传及预览 Form提交 Ajax 上传文件 时机: 如果发送的[文件]:->iframe, jQurey(),伪Ajax 预览 import os img_path = os.path.j ...

  8. android页面布局(listview填充中间)

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  9. 监控rman备份

    1.服务会话关联通道设置 set COMMAND ID 命令 2.查询V$PROCESS和V$SESSION 决定会话对应的RMAN的通道 3.查询V$session_LONGGOPS监控备份集和复制 ...

  10. ajax无刷新翻页后,jquery失效问题的解决

    例如 $(".entry-title a").click(function () {   只对第一页有效, 修改为 $(document).on('click', ".e ...