题目链接

题意 :

给出一个联通图和一些特殊的点,现在定义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. dubbo参数调优

    dubbo中配置优先级规律:方法级配置优先级高于接口级,consumer的优先级高于provider. 详细: consumer的method配置  >  provider的method配置 c ...

  2. =面试题:java面试基本方向 背1 有用 项目二技术学完再看

    一.Java基础 1. 集合框架A)集合中泛型优点? 将运行期的ClaasCastException 转到编译期异常.  泛型还提供通配符 1)HashMap---允许一个键为null,允许多个值为n ...

  3. suse10配置SSH无密码登录的方法

    RSH配置(集群中的每台机器执行以下操作) 1.因SUSE LINUX不自带RSH-SERVER服务,所以首先要去从www.rpmfind.net 下载rsh-server服务的RPM包. 然后切换到 ...

  4. 第04章-面向切面的Spring

    1. 什么是面向切面编程 AOP是什么 切面帮助我们模块化横切关注点. 横切关注点可被描述为影响应用[多处的]功能.如安全,应用许多方法会涉及安全规则. 继承与委托是最常见的实现重用 通用功能 的面向 ...

  5. 编写高质量代码改善C#程序的157个建议——建议25:谨慎集合属性的可写操作

    建议25:谨慎集合属性的可写操作 如果类型的属性中有集合属性,那么应该保证属性对象是由类型本身产生的.如果将属性设置为可写,则会增加抛出异常的几率.一般情况下,如果集合属性没有值,则它返回的Count ...

  6. 加载 页面 中js 方法

    js 文件中 var mingchen= mingchen|| {    init: function (){ } }; 文件中 mingchen.init(); 注意问题: 在新加载 页面中     ...

  7. poj2299 Ultra-QuickSort(线段树求逆序对)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  8. delphi 创建DLL文件 及其调用和注意事项

    首先创建一个DLL文件,项目自带的代码为: library ProjectPnr; { Important note about DLL memory management: ShareMem mus ...

  9. arcconf工具相关命令V1.0

    arcconf工具相关命令V1.0 清除当前所有raid配置 Arcconf  delete  1  array  all       #删除所有逻辑盘 Arcconf  uninit  1  all ...

  10. [.net 多线程 ]ReaderWriterLock

    ReaderWriterLock 用于同步对资源的访问.在任一特定时刻,它允许多个线程同时进行读访问,或者允许单个线程进行写访问.在资源不经常发生更改的情况下,ReaderWriterLock 所提供 ...