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> ...
随机推荐
- k8s日志收集配置
容器日志样例 172.101.32.1 - - [03/Jun/2019:17:14:10 +0800] "POST /ajaxVideoQueues!queryAllUser.action ...
- 【BZOJ 1150】[CTSC2007]数据备份Backup
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 选择的连接肯定是相邻的点对. 那么我们处理出来长度为n-1的数组a 其中a[i-1] = dis[i]-dis[i-1] 那么问题就 ...
- Google的10大座右铭
1. 以用户为中心,其他一切纷至沓来. 创建伊始,Google 即以提供最佳的用户体验为其中心任务.虽然很多公司主张客户利益优先,但难以抗拒各种诱惑,往往会牺牲客户的少量利益来增加股东价值. Goog ...
- Yocto tips (15): Yocto中的包管理器
使用包管理器 在local.conf中使能就可以: 然后编译后就会有rpm包了: 配置文件server 能够使用ngix和apache.可是我们也能够仅仅用使用python: python -m Si ...
- iOS开发- Images can’t contain alpha channels or transparencies 解决的方法
新版的iTunesConnect真是各种问题阿... 蛋疼要命. 上传介绍图片(屏幕截图)的时候 , 遇到了这个问题 Images can't contain alpha channels or tr ...
- ural 1143. Electric Path(凸包上最短哈密顿路径)
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1143 题意:逆时针给一个凸包的n(n<=200)个顶点坐标,求一个最短哈密顿路径的 ...
- 从设计稿到demo
前言 先省略产品设计师的wireframe和mockup,为什么线框图和视觉设计稿不在叙述范围内,由于这里要做的是网页重构,从接收设计师的PSD那一刻開始: 一.接到设计稿 1.接收到PSD后.不要着 ...
- MongoDB,SpringBoot,SpringDataMongoDB
MongoDB,SpringBoot,SpringDataMongoDB 双刃剑MongoDB的学习和避坑 MongoDB 是一把双刃剑,它对数据结构的要求并不高.数据通过key-value的形式存储 ...
- [USACO09JAN] 气象测量/气象牛The Baric Bovine 解题报告(DP)
题目链接:https://www.luogu.org/problemnew/show/P2933 Description 为了研究农场的气候,Betsy帮助农夫John做了N(1 <= N &l ...
- POJ 3204 网络流的必须边
思路: 求一遍网络流 在残余网络上DFS 从起点DFS 从终点把边反向DFS 一个边跟起点连通 跟终点反向的边连通 ans++ 注:此题不能用tarjan 因为有边权为0的边 //By SiriusR ...