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> ...
随机推荐
- nginx 多级7层代理安装配置
编译安装 yum install zlib-devel -y wget https://nginx.org/download/nginx-1.15.12.tar.gz tar -zxf nginx-1 ...
- Laravel关联模型中过滤结果为空的结果集(has和with区别)
首先看代码: $userCoupons = UserCoupons::with(['coupon' => function($query) use($groupId){ return $quer ...
- Oracle学习总结(7)—— 常用的数据库索引优化语句总结
不管是用C/C++/Java等代码编写的程序,还是SQL编写的数据库脚本,都存在一个持续优化的过程.也就是说,代码优化对于程序员来说,是一个永恒的话题. 近期,我们对之前编写的数据库脚本进行了全面的自 ...
- Tomcat连HBase报错: HTTP Status 500 - java.lang.AbstractMethodError: javax.servlet.jsp.JspFactory.getJspApplicationContext
Tomcat中连接HBase数据库,启动的时候报错: HTTP Status 500 - java.lang.AbstractMethodError: javax.servlet.jsp.JspFac ...
- win7休眠的开启与关闭方法命令行操作和图文结合的鼠标操作
win7休眠的开启与关闭方法 从開始菜单中找到"附件→命令提示符",手工输入例如以下命令:powercfg -a.从这里能够清楚的看到,计算机是支持休眠的.显示"尚未启用 ...
- 【我所认知的BIOS】系列blog整理 1.23.2016.zip
这几年来,蛮多小伙伴都给我发邮件拿PDF版本号. 几年前写的文章格式什么的实在是太粗糙.近期我把全部的文章都整理了一下.事实上该想法已经早就有了,仅仅是近期才開始空暇.如今我把全部的文章整理好了以后上 ...
- 使用bbed恢复表数据
对于表级别的数据恢复,ORACLE提供了多种恢复方法:flashback query,logmnr等. 本文通过演示样例演示使用bbed的copy命令恢复用户误删除或者损坏的表数据,当然我们也能够使用 ...
- java生成MD5校验码
在Java中,java.security.MessageDigest (rt.jar中)已经定义了 MD5 的计算,所以我们只需要简单地调用即可得到 MD5 的128 位整数.然后将此 128 位计 ...
- 46. AngularJS所有版本下载
转自:https://www.cnblogs.com/best/tag/Angular/ 官网下载:https://angularjs.org/ AngularJS所有版本下载:https://cod ...
- Strings are immutable
It is tempting to use the [] operator on the left side of an assignment, with the intention of chang ...