题意:平面上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 最小生成树的更多相关文章

  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. POJ——T2117 Electricity

     http://poj.org/problem?id=2117 Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5459   ...

  2. jsp urlrewrite 中正則表達式不包括某个字符串写法

    因在程序中须要做城市间跳转,可是页面中包括的css.scripts和图片等路径是要排除在外的. 这就须要在正则中指定当遇到哪些 字符时须要略过. 正则例如以下: /((? !css)(?!script ...

  3. lenovo G系列重装系统

    lenovo G41 的笔记本默认安装的是win8 中文版 的操作系统,使用非常不方便,用U盘重装成WIN7的系统. 1.用启动工具软件制作U盘启动盘.  详细能够參照  http://www.uqi ...

  4. Android SQLite 简单使用演示样例

    SQLite简单介绍 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也很的强大. 袖珍型的SQLite能够支持高达2TB大小的数据库, ...

  5. C++学习第一天--编译命令

    前一个月的时间主要是在捯饬自己的ubuntu vim环境,昨天终于都搞好了,从今天开始,学习C++.至于为什么学习C++,其实很大一部分原因还是因为自己喜欢vim,又听说vim对C++的支持还不错,所 ...

  6. Pocket英语语法---二、指示代词和不定代词是什么

    Pocket英语语法---二.指示代词和不定代词是什么 一.总结 一句话总结: 指示代词:标识人或事物的代词,用来代替前面已提到过的名词 this.these.that.those不定代词:指代不确定 ...

  7. zzulioj--1817--match number(水题)

    1817: match number Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 98  Solved: 45 SubmitStatusWeb Bo ...

  8. Android--Fragment与Activity通信

    package com.example.testfragment; import com.example.testfragment.MainFargment.BackString; import an ...

  9. BZOJ 3209 数位DP

    思路: 先预处理出来组合数 按位做 枚举sum[x]是多少 注意Mod不是一个质数 //By SiriusRen #include <cstdio> using namespace std ...

  10. NestedScrollView嵌套ListView可行性总结

    由于公司项目遗留代码仍然使用PullToRefreshListView(后文简称PTRLV),且存在复用,更换RecyclerView成本太大,同时又想使用CoordinatorLayout来实现一些 ...