原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf

题解

给你平面上若干点,生成一颗完全图,让你生成一颗最小生成树。模板题。图中已经有了的边要将权值置0。代码是队友写的。

代码

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <bitset>
#define INF 1000000005
#define eps 1e-10
#define PI acos(-1.0)
#define K (0.017453292519943295769236907684886l)
#define LL long long
#define ULL unsigned long long using namespace std; const int maxn = ; double a[maxn][maxn], dist[maxn]; int n, pre[maxn], m, x[maxn], y[maxn], vis[maxn][maxn], flag[maxn]; double Get_dist(int s, int t)
{
return sqrt((x[s] - x[t]) * (x[s] - x[t]) + (y[s] - y[t]) * (y[s] - y[t]));
} int main()
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
scanf("%d%d", &x[i], &y[i]);
memset(vis, , sizeof(vis));
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
a[i][j] = Get_dist(i, j);
scanf("%d", &m);
for (int i = ; i <= m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
vis[u][v] = vis[v][u] = ;
a[u][v] = a[v][u] = ;
}
for (int i = ; i <= n; i++)
{
flag[i] = ;
dist[i] = a[][i];
pre[i] = ;
}
flag[] = ; pre[] = ;
for (int i = ; i < n; i++)
{
double minx = 1e30;
int p;
for (int j = ; j <= n; j++)
if (flag[j] && minx > dist[j])
{
minx = dist[j];
p = j;
}
if (!vis[p][pre[p]]) printf("%d %d\n", p, pre[p]);
flag[p] = ;
for (int j = ; j <= n; j++)
if (dist[j] > a[p][j])
{
dist[j] = a[p][j];
pre[j] = p;
}
}
return ;
}

Codeforces Gym 100203H Highways 最小生成树的更多相关文章

  1. Gym - 100203H Highways 最小生成树

    题意:平面上n个点修路,已经修好了m条,再修若干条使得点之间连通,求最小代价的方案. 思路:基本上是裸的最小生成树了,我这里存边直接存在multyset了,取的时候也比较方便,我本来就是这么考虑的,队 ...

  2. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  3. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  4. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  5. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  6. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  7. codeforces gym 100553I

    codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...

  8. CodeForces Gym 100213F Counterfeit Money

    CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...

  9. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

随机推荐

  1. 补之前 如何改变jupyter打开文件的路径

    目录 如何改变jupyter打开文件的路径 第一种方法: 第二种方法 第三种方法 如何改变jupyter打开文件的路径 当我们直接打开jupyter时,直接加载的是我们的C盘文件 现在我们想打开其他盘 ...

  2. Applied Nonparametric Statistics-lec8

    Ref:https://onlinecourses.science.psu.edu/stat464/print/book/export/html/11 additive model value = t ...

  3. R-codes-tips

    1. 在shell执行R文件 chmod 0755 file.R Rscript file.R 2.  载入数据 data(dune) 3. attach() 将data.frame添加到R的搜索路径 ...

  4. python模块之pickle

    和json不同的是: json只支持str,int,tuple,list,dict. pickle支持python里所有的数据类型,但是只能在python里序列化,不跨平台,python独有. 代码示 ...

  5. Linux学习-工作管理 (job control)

    什么是工作管理? 进行工作管理的行为中, 其实每个工作都是目前 bash 的 子进程,亦即彼此之间是有相关性的. 我们无法以 job control 的方式由 tty1 的环境去管理 tty2 的 b ...

  6. #1 add life to static pages && connect to MySQL

    由于实验室 Project 中需要用到PHP, 之前也没接触过 PHP, 因此把 编程入门 <Head Fist PHP & MySQL >找来花了四五天快速过了一遍. 现在想把书 ...

  7. 使用像AdminLTE的前端框架,树形导航菜单实现方式都有哪些?

    之前用easyui等富前端框架开发的时候都是使用封装好的县城的插件,现在使用最新的类似AdminLTE似的前段框架实现树形菜单都用什么方式? 后台拼接html然后前端用JS append方法添加还是直 ...

  8. install redis and used in golang on ubuntu 14.04

    $ wget http://download.redis.io/releases/redis-3.0.3.tar.gz$ tar xzf redis-3.0.3.tar.gz$ cd redis-3. ...

  9. 实现socket并发的几种方法

    # 使用多进程实现socket聊天并发-server #服务端 import socket from multiprocessing import Process def server(conn,ad ...

  10. [POJ 1000] A+B Problem 经典水题 C++解题报告 JAVA解题报告

        A+B Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 311263   Accepted: 1713 ...