Avito Cool Challenge 2018:D. Maximum Distance (最小生成树)
题意 :
给出一个联通图和一些特殊的点,现在定义cost(u,v)为一条从u到v的路径上面边权的最大值 ,
定义dis(u,v) 为从u到v 路径上面cost 的最小值
然后求所有特殊点到其他特殊点的最大距离
题解:
做这题前,首先思考一件事情,对于一颗树来说点到点的距离是不是就是树上面路径的边权最大值
我们来证明一下:假设在最小生成树上面的路径cost为w1,另外在原图中还有一条路径从u到v,其cost为w2,那么必然有w2>w1的。那么我们最后的dis一定是w1。
那么我们现在的目标就是求特殊点到特殊点之间的最大距离。注意一下这里是从一个特殊点到其它所有特殊点的最大距离
我们根据Kruskal 算法的构建过程 , 在构建树的时候是先构造小的边的 , 所以我们就可以在Kruskal加边的时候更新答案 ,
我们假设现在有两个集合,现在将其连接起来,当满足两个集合里面都有特殊点时我们就可以更新答案了,否则就不行。
转载 现在还有一些问题没有解决 , 待后跟新
#include<bits/stdc++.h>
using namespace std; const int maxn = ;//最大点数
int c[maxn], N,M,k;//并查集使用
int cnt;
bool a[maxn];
int VAL[maxn];
struct EDGE{
int from, to, w;
bool operator < (const EDGE &rhs) const{
return this->w < rhs.w;
};
}Edge[maxn];//储存边的信息,包括起点/终点/权值 inline void init()
{
for(int i=; i<=N; i++)
c[i] = i;
cnt = ;
} inline void AddEdge(int from, int to, int weight)
{
Edge[cnt].from = from;
Edge[cnt].to = to;
Edge[cnt].w = weight;
cnt++;
} int Findset(int x)
{
int root = x;
while(c[root] != root)
root = c[root]; int idx;
while(c[x] != root){ /// 路径压缩
idx = c[x];
c[x] = root;
x = idx;
}
return root;
} int Kruskal()//传入点数,返回最小生成树的权值,如果不连通返回-1
{
sort(Edge,Edge+cnt);
int EdgeCnt=;//计算加入的边数
int Cost=;
int MAX=;
for(int i=;i<cnt;i++){
int u=Edge[i].from;
int v=Edge[i].to;
int w=Edge[i].w;
int R1 = Findset(u);
int R2 = Findset(v);
if(R1==R2) continue;
c[R1]=R2;
if(a[u]) VAL[R1]++;//标记的点
if(a[v]) VAL[R2]++;
if(VAL[R1] && VAL[R2] )//如果标记的点都有
MAX=w;
VAL[R2]+=VAL[R1];
EdgeCnt++;
if(EdgeCnt==N-) break;
}
if(EdgeCnt<N-) return -;//不连通
else return MAX;
} int main()
{
scanf("%d%d%d",&N,&M,&k);
init();
int Val;
for(int i= ; i<=k ; i++)
{
scanf("%d",&Val);
a[Val]=;
} for(int i= ; i<=M ; i++)
{ int u,v,w;
scanf("%d%d%d",&u,&v,&w);
AddEdge(u,v,w);
}
int P=Kruskal();
for(int i= ; i<=k ; i++)
printf("%d ",P);
// printf("%d\n", Kruskal()); return ;
}
Avito Cool Challenge 2018:D. Maximum Distance (最小生成树)的更多相关文章
- Codeforces Avito Code Challenge 2018 D. Bookshelves
Codeforces Avito Code Challenge 2018 D. Bookshelves 题目连接: http://codeforces.com/contest/981/problem/ ...
- Avito Cool Challenge 2018:D. Maximum Distance
D. Maximum Distance 题目链接:https://codeforces.com/contest/1081/problem/D 题意: 给出一个连通图以及一些特殊点,现在定义cost(u ...
- Avito Cool Challenge 2018
考挂了.. A - Definite Game 直接看代码吧. #include<cstdio> #include<cstring> #include<algorithm ...
- Avito Cool Challenge 2018(div1+2)
A. Definite Game: 题意:输入N,输出最小的结果N-x,其中x不少N的因子. 思路:N=2时,输出2:其他情况输出1:因为N>2时,N-1不会是N的因子. #include< ...
- Avito Cool Challenge 2018 Solution
A. Definite Game 签. #include <bits/stdc++.h> using namespace std; int main() { int a; while (s ...
- Avito Code Challenge 2018
第一次打CF,很菜,A了三道水题,第四题好像是是数位DP,直接放弃了.rateing从初始的1500变成了1499,还是绿名,这就很尴尬.之后觉得后面的题目也没有想象的那么难(看通过人数)过两天吧剩下 ...
- Avito Cool Challenge 2018 自闭记
A:n==2?2:1. #include<iostream> #include<cstdio> #include<cmath> #include<cstdli ...
- Avito Cool Challenge 2018 E. Missing Numbers 【枚举】
传送门:http://codeforces.com/contest/1081/problem/E E. Missing Numbers time limit per test 2 seconds me ...
- Avito Cool Challenge 2018 C. Colorful Bricks 【排列组合】
传送门:http://codeforces.com/contest/1081/problem/C C. Colorful Bricks time limit per test 2 seconds me ...
随机推荐
- module 'keras.engine.topology' has no attribute 'load_weights_from_hdf5_group_by_name'
参考: https://blog.csdn.net/heiheiya/article/details/81111932 https://blog.csdn.net/c20081052/article/ ...
- 面试题:hibernate 有用
1. Hibernate的工作流程? 答案: 1.通过Configuration对象读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory对象 3.打开session 4.创建事 ...
- 1.sql简介
在总结sql语句前,说点无聊的哈哈 SQL 是用于访问和处理数据库的标准的计算机语言. SQL 能做什么? SQL 面向数据库执行查询 SQL 可从数据库取回数据 SQL 可在数据库中插入新的记录 S ...
- 截取utf8中文字符串
英文直接截取即可. 中文应字节长度会乱码,应先转unicode截取. 如下: #-*- coding:utf8 -*- s = u'截取中文' s.decode('utf8')[0:3].encode ...
- redis的一些简介
Redis是Remote Dictionary Server的缩写,他本质上一个Key/Value数据库,与Memcached类似的NoSQL型数据库. 1. redis的数据类型: st ...
- golang subprocess tests
golang Subprocess tests Sometimes you need to test the behavior of a process, not just a function. f ...
- Web标准及网站的可用性、可访问性
学习前端的过程中到处充斥着Web标准.可用性.可访问性这些词,那么到底它们指的是什么呢? 一.什么是Web标准 简单的说,Web标准就是我们在学习前端中接触最多的HTML.CSS.JavaScript ...
- spring @Async 线程池使用
最近公司项目正逐渐从dubbo向springCloud转型,在本次新开发的需求中,全部使用springcloud进行,在使用时线程池,考虑使用spring封装的线程池,现将本次使用心得及内容记录下来 ...
- 20165219 《Java程序设计》实验一(Java开发环境的熟悉)实验报告
20165219 <Java程序设计>实验一(Java开发环境的熟悉)实验报告 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:王彦博 学号:20165219 成绩: 指 ...
- IIS发布ASP程序问题汇总
看异常位置,因为域的问题