题目链接

题意 :

给出一个联通图和一些特殊的点,现在定义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. springboot @Value 类中读取配置文件 .properties null 原因和解决方案

    问题:在一个工具类中,通过@Value来映射配置文件的值,得到的总是null 原因:不能用new工具类的方式,应该是用容器注册(@Autowried)的方式使用此工具类,就能得到配置文件里的值 上代码 ...

  2. Ionic01 简单介绍、环境搭建、创建项目、项目结构、创建组件、创建页面、子页面跳转

    1 Ionic 基本介绍 Ionic 是一款基于 Angular.Cordova 的强大的 HTML5 移动应用开发框架 , 可以快速创建一个跨平台的移动应用.可以快速开发移动 App.移动端 WEB ...

  3. 243. Shortest Word Distance 最短的单词index之差

    [抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...

  4. 4-拷贝我的eclipse写安卓的配置说明

    1.下载加压: 2.配置关于jdk的javahome路径,配置过eclipse的到这里就可以了,否则百度ecplise安装配置环境变量即可: 3.以安卓项目方式加入appcompat-v7; 4.每次 ...

  5. PC建立WIFI热点

    netsh wlan set hostednetwork ssid=test key =12345678netsh wlan start hostednetwork

  6. setnx()

    setnx(key,value):当指定的key不存在时,为你设置指定的值

  7. Mybatis——缓存机制

    MyBatis 包含一个非常强大的查询缓存特性,它可以非常方便地配置和定制.缓存可以极大的提升查询效率. MyBatis系统中默认定义了两级缓存. 一级缓存和二级缓存. 1.默认情况下,只有一级缓存( ...

  8. Visual Studio OpenCV 开发环境配置

    因为VS配置OpenCV好多新手都很难一次配置成功,而且OpenCV库每新建一个项目都要配置很是麻烦,所以今天就给大家介绍一个“一劳永逸”的方法. 注:理论上只要VS和OpenCV是版本兼容的,该方法 ...

  9. HTML 5+CSS 3网站布局应用教程 (赵振方) 随书光盘 ​

    <HTML5+CSS3网站布局应用教程>全面介绍HTML 5与CSS 3进行Web设计的知识.全书由16章组成.主要内容包括:主流浏览器对HTML 5的支持情况.HTML 5与HTML4在 ...

  10. 在云主机后台进行python程序运行

    nohup python main.py & nohup liunx自带的命令 注意:后面(&)!