Codeforces Gym 100203H Highways 最小生成树
原题链接: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 最小生成树的更多相关文章
- Gym - 100203H Highways 最小生成树
题意:平面上n个点修路,已经修好了m条,再修若干条使得点之间连通,求最小代价的方案. 思路:基本上是裸的最小生成树了,我这里存边直接存在multyset了,取的时候也比较方便,我本来就是这么考虑的,队 ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- 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 ...
- codeforces gym 100553I
codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
- CodeForces Gym 100213F Counterfeit Money
CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
随机推荐
- tuple 方法总结整理
#!/usr/bin/env python #Python 3.7.0 元祖常用方法 __author__ = "lrtao2010" #元祖和列表类似,只不过元祖一旦被创建一级元 ...
- console_init()分析
启动阶段初始化控制台流程分析, start_kernel console_init(); -->tty_ldisc_begin(); /* Setup the default TTY line ...
- PAT Basic 1074
1074 宇宙无敌加法器 地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为“PAT数”.每个 PAT 星 ...
- 制作iso文件
genisoimage -o fusionstor-yi-2017-03-08.iso(镜像名称) -v -cache-inodes -joliet-long -R -J -T -V ZS -c ...
- HDU 4812 D Tree 树分治
题意: 给出一棵树,每个节点上有个权值.要找到一对字典序最小的点对\((u, v)(u < v)\),使得路径\(u \to v\)上所有节点权值的乘积模\(10^6 + 3\)的值为\(k\) ...
- P1627 中位数
P1627 中位数 题目描述 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 输入输出格式 输入格式: 第一行为两个正整 ...
- Ubuntu下安装anaconda和pycharm
折腾了一上午,终于装好了,如下:Python环境的安装: 安装anaconda 建议去https://www.anaconda.com/download/#linux直接用Ubuntu界面的搜狐浏览器 ...
- dubbo Protocol实现剖析
title: dubbo Protocol实现剖析 date: 2018-09-09 19:10:07 tags: --- 2.6.3版本,之前读的是2.4.9版本 本篇主要阐述dubbo rpc的c ...
- 大数据学习——scala集合练习
package com /** * Created by ZX on 2016/4/5. */ object ListTest { def main(args: Array[String]) { // ...
- selenium - js日历控件处理
# 13. js处理日历控件 ''' 在web自动化的工程中,日历控制大约分为两种: 1. 可以直接输入日期 2. 通过日历控件选择日期 基本思路: 利用js去掉readonly属性,然后直接输入时间 ...