Gym - 100203H Highways 最小生成树
题意:平面上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 最小生成树的更多相关文章
- Codeforces Gym 100203H Highways 最小生成树
原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 给你平面上若干点,生成一颗完全图,让 ...
- POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)
...
- POJ 2485 Highways 最小生成树 (Kruskal)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- POJ 1751 Highways(最小生成树Prim普里姆,输出边)
题目链接:点击打开链接 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has ...
- poj 2485 Highways 最小生成树
点击打开链接 Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19004 Accepted: 8815 ...
- POJ 1751 Highways (最小生成树)
Highways Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- spoj 104 Highways (最小生成树计数)
题目链接:http://www.spoj.pl/problems/HIGH/ 题意:求最小生成树个数. #include<algorithm> #include<cstdio> ...
- POJ2485 Highways(最小生成树)
题目链接. 分析: 比POJ2253要简单些. AC代码: #include <iostream> #include <cstdio> #include <cstring ...
- 最小生成树练习3(普里姆算法Prim)
风萧萧兮易水寒,壮士要去敲代码.本女子开学后再敲了.. poj1258 Agri-Net(最小生成树)水题. #include<cstdio> #include<cstring> ...
随机推荐
- 【转载】spring boot 链接 虚拟机(Linux) redis
原文:https://www.imooc.com/article/43279?block_id=tuijian_wz 前提是你已经安装redis且支持远程连接,redis的安装这里不再赘述,有需要的可 ...
- 一个 VUE 组件:实现子元素 scroll 父元素容器不跟随滚动(兼容PC、移动端)
介绍 我们经常遇到一种情况.当滑动滚动条区域时,子元素滚动条到底部或顶部时就会触发父级滚动条,父级滚动条同理会继续向上触发,直至body容器.这是浏览器默认的滚动行为. 但是很多情况,我们想要子元素滚 ...
- ESRI.ArcGIS.Controls.AxMapControl
今天在写DLL时发现,直接引用ESRI.ArcGIS.Controls,发现AxMapControl的参数仍然不好用,后来发现,需要引用ESRI.ArcGIS.AxControls这个DLL.而且还需 ...
- APK反编译去广告大揭秘
APK反编译去广告 具体步骤: 1.下载 apktool 下载地址:https://code.google.com/p/android-apktool/downloads/list 2.通过apkto ...
- hdu 2102 A计划 具体题解 (BFS+优先队列)
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
- Android中的跨进程通信方法实例及特点分析(一):AIDL Service
转载请注明出处:http://blog.csdn.net/bettarwang/article/details/40947481 近期有一个需求就是往程序中增加大数据的採集点,可是由于我们的Andro ...
- js面向对象编程:怎样定义常量?
js中有一个keywordconst,但眼下的浏览器似乎还不支持,假设一定要定义一些常量,事实上能够使用闭包,匿名函数实现常量的定义. 比如: var Class = (function() { va ...
- hdoj--5567--sequence1(水题)
sequence1 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- Nginx中的upstream 分配方法
轮询 轮询是upstream的默认分配方式,即每个请求按照时间顺序轮流分配到不同的后端服务器,如果某个后端服务器down掉后,能自动剔除. upstream www_cc_com { server 1 ...
- Metasploit的armitage初步使用
armitage的启动 root@kali:~# armitage 别急,过会儿就好了 . 等扫描完会弹出一个框框然后会多出目标的图标比如目标是打印机