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

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

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <fstream>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <deque>
  7. #include <vector>
  8. #include <queue>
  9. #include <string>
  10. #include <cstring>
  11. #include <map>
  12. #include <stack>
  13. #include <set>
  14. #define LL long long
  15. #define eps 1e-8
  16. #define INF 0x3f3f3f3f
  17. #define MAXN 755
  18. using namespace std;
  19. struct Edge{
  20. int from, to;
  21. LL dis;
  22. Edge(int from, int to, LL dis):from(from), to(to), dis(dis){};
  23. bool operator <(const Edge &b) const{
  24. return dis < b.dis;
  25. }
  26. };
  27. multiset<Edge> s;
  28. struct Point{
  29. int x, y;
  30. }p[MAXN];
  31. LL dis[MAXN][MAXN];
  32. bool vis[MAXN][MAXN];
  33. int father[MAXN];
  34. int scan(){
  35. int res = , ch, flag = ;
  36.  
  37. if((ch = getchar()) == '-')
  38. flag = ;
  39.  
  40. else if(ch >= '' && ch <= '')
  41. res = ch - '';
  42. while((ch = getchar()) >= '' && ch <= '' )
  43. res = res * + ch - '';
  44.  
  45. return flag ? -res : res;
  46. }
  47. LL getdis(Point a, Point b){
  48. LL x = abs(a.x - b.x);
  49. LL y = abs(a.y - b.y);
  50. return x * x + y * y;
  51. }
  52. int find(int x){
  53. if(father[x] == x) return x;
  54. father[x] = find(father[x]);
  55. return father[x];
  56. }
  57. int main()
  58. {
  59. #ifndef ONLINE_JUDGE
  60. freopen("in.txt", "r", stdin);
  61. //freopen("out.txt", "w", stdout);
  62. #endif // OPEN_FILE
  63. int n = scan();
  64. for(int i = ; i <= n; i++){
  65. father[i] = i;
  66. p[i].x = scan();
  67. p[i].y = scan();
  68. }
  69. s.clear();
  70. LL dis;
  71. for(int i = ; i <= n; i++){
  72. for(int j = i + ; j <= n; j++){
  73. dis = getdis(p[i] ,p[j]);
  74. s.insert(Edge(i, j, dis));
  75. }
  76. }
  77. int m =scan();
  78. int x, y;
  79. for(int i = ; i <= m; i++){
  80. x = scan();
  81. y = scan();
  82. vis[x][y] = true;
  83. x = find(x);
  84. y = find(y);
  85. if(x == y) continue;
  86. father[x] = y;
  87. }
  88. multiset<Edge>::iterator it = s.begin();
  89. while(it != s.end()){
  90. Edge u = *it;
  91. it++;
  92. if(vis[u.from][u.to] || vis[u.to][u.from]) continue;
  93. x = find(u.from);
  94. y = find(u.to);
  95. if(x == y) continue;
  96. father[x] = y;
  97. printf("%d %d\n", u.from, u.to);
  98. }
  99.  
  100. }

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. nginx 多级7层代理安装配置

    编译安装 yum install zlib-devel -y wget https://nginx.org/download/nginx-1.15.12.tar.gz tar -zxf nginx-1 ...

  2. Laravel关联模型中过滤结果为空的结果集(has和with区别)

    首先看代码: $userCoupons = UserCoupons::with(['coupon' => function($query) use($groupId){ return $quer ...

  3. Oracle学习总结(7)—— 常用的数据库索引优化语句总结

    不管是用C/C++/Java等代码编写的程序,还是SQL编写的数据库脚本,都存在一个持续优化的过程.也就是说,代码优化对于程序员来说,是一个永恒的话题. 近期,我们对之前编写的数据库脚本进行了全面的自 ...

  4. 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 ...

  5. win7休眠的开启与关闭方法命令行操作和图文结合的鼠标操作

    win7休眠的开启与关闭方法 从開始菜单中找到"附件→命令提示符",手工输入例如以下命令:powercfg -a.从这里能够清楚的看到,计算机是支持休眠的.显示"尚未启用 ...

  6. 【我所认知的BIOS】系列blog整理 1.23.2016.zip

    这几年来,蛮多小伙伴都给我发邮件拿PDF版本号. 几年前写的文章格式什么的实在是太粗糙.近期我把全部的文章都整理了一下.事实上该想法已经早就有了,仅仅是近期才開始空暇.如今我把全部的文章整理好了以后上 ...

  7. 使用bbed恢复表数据

    对于表级别的数据恢复,ORACLE提供了多种恢复方法:flashback query,logmnr等. 本文通过演示样例演示使用bbed的copy命令恢复用户误删除或者损坏的表数据,当然我们也能够使用 ...

  8. java生成MD5校验码

    在Java中,java.security.MessageDigest (rt.jar中)已经定义了 MD5 的计算,所以我们只需要简单地调用即可得到 MD5 的128 位整数.然后将此 128 位计 ...

  9. 46. AngularJS所有版本下载

    转自:https://www.cnblogs.com/best/tag/Angular/ 官网下载:https://angularjs.org/ AngularJS所有版本下载:https://cod ...

  10. Strings are immutable

    It is tempting to use the [] operator on the left side of an assignment, with the intention of chang ...