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> ...
随机推荐
- 中国象棋程序的设计与实现(六)--N皇后问题的算法设计与实现(源码+注释+截图)
八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题. 该问题是十九世纪著名的数学家高斯1850年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列 ...
- 【BZOJ 1269】 [AHOI2006]文本编辑器editor
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] /* [move k] 指令.直接 把pos改成k.表示改变光标位置 [insert n s],在pos后面插入一个长度为n的字符串 ...
- Redis学习总结(3)——Redis整合Spring结合使用缓存实例
摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的方法切入到有需要进入缓存的类或方法前面. 一.Redis介绍 什么是Redis? redis是一个key- ...
- 【转】一天学会PHP(转)
[转]一天学会PHP(转) 只需要一天,只要你用心去看和学,一定行. - 这里希望大家需要明白一点,这只是在讲如何快速入门,更好的认识PHP!也能初级掌握PHP基础知识!PHP语言博大精深!并不是一两 ...
- [Test] Easy automated testing in NodeJS with TestCafe
Quickly get up and running with sensible automated testing scenarios written in ES6. Installing and ...
- 【我所认知的BIOS】系列blog整理 1.23.2016.zip
这几年来,蛮多小伙伴都给我发邮件拿PDF版本号. 几年前写的文章格式什么的实在是太粗糙.近期我把全部的文章都整理了一下.事实上该想法已经早就有了,仅仅是近期才開始空暇.如今我把全部的文章整理好了以后上 ...
- Func委托和Action委托
http://stackoverflow.com/questions/4317479/func-vs-action-vs-predicate The difference between Func a ...
- reverse(两种反向生成url django原生形式和rest_framework中版本的形式)
reverse(两种反向生成url django原生形式和rest_framework中版本的形式) views.py from django.shortcuts import render,Http ...
- linux中的raid
参考文档 http://www.cnblogs.com/ivictor/p/6099807.html 制作raid5 http://blog.51cto.com/11134648/2103384 RA ...
- 8.queue
#include <iostream> #include <stack> #include <algorithm> #include <list> #i ...