题目链接

题意 :

给出一个联通图和一些特殊的点,现在定义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 (最小生成树)的更多相关文章

  1. Codeforces Avito Code Challenge 2018 D. Bookshelves

    Codeforces Avito Code Challenge 2018 D. Bookshelves 题目连接: http://codeforces.com/contest/981/problem/ ...

  2. Avito Cool Challenge 2018:D. Maximum Distance

    D. Maximum Distance 题目链接:https://codeforces.com/contest/1081/problem/D 题意: 给出一个连通图以及一些特殊点,现在定义cost(u ...

  3. Avito Cool Challenge 2018

    考挂了.. A - Definite Game 直接看代码吧. #include<cstdio> #include<cstring> #include<algorithm ...

  4. Avito Cool Challenge 2018(div1+2)

    A. Definite Game: 题意:输入N,输出最小的结果N-x,其中x不少N的因子. 思路:N=2时,输出2:其他情况输出1:因为N>2时,N-1不会是N的因子. #include< ...

  5. Avito Cool Challenge 2018 Solution

    A. Definite Game 签. #include <bits/stdc++.h> using namespace std; int main() { int a; while (s ...

  6. Avito Code Challenge 2018

    第一次打CF,很菜,A了三道水题,第四题好像是是数位DP,直接放弃了.rateing从初始的1500变成了1499,还是绿名,这就很尴尬.之后觉得后面的题目也没有想象的那么难(看通过人数)过两天吧剩下 ...

  7. Avito Cool Challenge 2018 自闭记

    A:n==2?2:1. #include<iostream> #include<cstdio> #include<cmath> #include<cstdli ...

  8. Avito Cool Challenge 2018 E. Missing Numbers 【枚举】

    传送门:http://codeforces.com/contest/1081/problem/E E. Missing Numbers time limit per test 2 seconds me ...

  9. Avito Cool Challenge 2018 C. Colorful Bricks 【排列组合】

    传送门:http://codeforces.com/contest/1081/problem/C C. Colorful Bricks time limit per test 2 seconds me ...

随机推荐

  1. sql语句查询中exists中为什么要用select 1?

    select * from call_cdr_xz_200609 a where and a.ori_charge<>0 and exists(select 1 from special ...

  2. Python学习笔记_Python向Excel写入数据

    实验环境 1.OS:Win 10 64位 2.Python 3.7 3.如果没有安装xlwt库,则安装:pip install xlwt 下面是从网上找到的一段代码,网上这段代码,看首行注释行,是在L ...

  3. Linux gtypist

    一.简介 Typist (gtypist)是一个打字练习软件,用来提升打字的速度.   二.安装 1)源码方式 http://ftp.gnu.org/gnu/gtypist/   三.使用 http: ...

  4. osm2pgsql导入少字段

    Explanation: osm2pgsql imports normally the data in a static database schema. The tags without a cor ...

  5. python文件复制移动shutil模块

    shutil.copyfile( src, dst) 从源src复制到dst中去.当然前提是目标地址是具备可写权限.抛出的异常信息为IOException. 如果当前的dst已存在的话就会被覆盖掉 s ...

  6. hdu 4269 Defend Jian Ge

    #include <cctype> #include <algorithm> #include <vector> #include <string> # ...

  7. 1.python系统性能信息模块之psutil模块

    Psutil(进程和系统实用程序)是一个跨平台的库,用于在Python中检索有关运行进程和系统资源利用率(CPU,内存,磁盘,网络)的信息.它主要用于系统监视,分析和限制系统资源及运行进程的管理.它实 ...

  8. 关于AJAX与JSON的杂记

    一.当网页需要有多个XMLHttpRequest对象时,可以使用Callback 函数,callback 函数是一种以参数形式传递给另一个函数的函数. <html> <head> ...

  9. Arduino I2C + 数字式环境光传感器BH1750FVI

    BH1750FVI是日本罗姆(ROHM)半导体生产的数字式环境光传感IC.其主要特性有: I2C数字接口,支持速率最大400Kbps 输出量为光照度(Illuminance) 测量范围1~65535 ...

  10. js作用域解析原理

    当代码进入到<script>标签或者在调用一个方法,那么就会进入作用域,在解析代码的时候就会做以下两件事情: ①去找var 和function关键字进行js预解析如果有var把值全部定义成 ...