Avito Cool Challenge 2018:D. Maximum Distance
D. Maximum Distance
题目链接:https://codeforces.com/contest/1081/problem/D
题意:
给出一个连通图以及一些特殊点,现在定义cost(u,v)为一条从u到v的路径上面边权的最大值,然后定义dis(u,v)为从u到v所有路径上面cost的最小值。
最后求所有特殊点到其它特殊点的最大距离...
题解:
这个题意似乎有点绕...
我们考虑一下最小生成树,那么点与点之间的距离就为最小生成树路径上面边权的最大值。
我们来证明一下:假设在最小生成树上面的路径cost为w1,另外在原图中还有一条路径从u到v,其cost为w2,那么必然有w2>w1的。那么我们最后的dis一定是w1。
那么我们现在的目标就是求特殊点到特殊点之间的最大距离。注意一下这里是从一个特殊点到其它所有特殊点的最大距离。
我们知道在Kruskal加边时,后加的边权一定时大于前面的边权的,既然要求最大权值,那么我们可以想加的最后一条边是否可以作为答案。
我们假设现在有两个集合,现在将其连接起来,当满足两个集合里面都有特殊点时我们就可以更新答案了,否则就不行。
所以我们合并的时候顺带维护一下集合里面特殊点的信息就可以了。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N =1e5+;
struct Edge{
int u,v,w;
bool operator < (const Edge&A)const{
return w<A.w;
}
}e[N];
int n,m,k;
int a[N],f[N],val[N];
int find(int x){
if(x==f[x]) return f[x];
f[x]=find(f[x]);
return f[x];
}
int main(){
cin>>n>>m>>k;
int tot=k;
for(int i=,t;i<=k;i++){
cin>>t;
a[t]=;
}
for(int i=;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
e[i]=Edge{u,v,w};
}
for(int i=;i<=n;i++) f[i]=i;
sort(e+,e+m+);
int ans;
for(int i=;i<=m;i++){
int u=e[i].u,v=e[i].v,w=e[i].w;
int fx=find(u),fy=find(v);
if(fx==fy) continue;
f[fx]=fy;
if(a[u]) val[fx]++;if(a[v]) val[fy]++;
if(val[fx]&&val[fy]) ans=w;
val[fy]+=val[fx];
}
for(int i=;i<=k;i++) cout<<ans<<" ";
return ;
}
Avito Cool Challenge 2018:D. Maximum Distance的更多相关文章
- Avito Cool Challenge 2018:D. Maximum Distance (最小生成树)
题目链接 题意 : 给出一个联通图和一些特殊的点,现在定义cost(u,v)为一条从u到v的路径上面边权的最大值 , 定义dis(u,v) 为从u到v 路径上面cost 的最小值 然后求所有特殊点到其 ...
- Avito Cool Challenge 2018:C. Colorful Bricks
C. Colorful Bricks 题目链接:https://codeforces.com/contest/1081/problem/C 题意: 有n个横向方块,一共有m种颜色,然后有k个方块的颜色 ...
- Codeforces Avito Code Challenge 2018 D. Bookshelves
Codeforces Avito Code Challenge 2018 D. Bookshelves 题目连接: http://codeforces.com/contest/981/problem/ ...
- 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 Cool Challenge 2018
考挂了.. A - Definite Game 直接看代码吧. #include<cstdio> #include<cstring> #include<algorithm ...
- 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 ...
随机推荐
- JAVA基础知识总结8(设计模式)
设计模式:JAVA中有23种设计模式 1.解决问题最行之有效的思想. 2.是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 3.使用设计模式是为了可重用代码.让代码更容易被他人理解 ...
- sql如何选取两个数据表中的值
一.直接在要选择的数据前面加上数据表的名字就行了 SELECT po.OrderID, p.LastName, p.FirstName FROM Persons AS p, Product_Order ...
- Android studio中ShredPreferences 的简单使用
ShredPreferences是一个轻量级的数据存储方式,只能存取字符串了整型数据这一类的数据,如果要存储复杂的数据这个存储方式就不再适用 首先是要新建一个ShredPreferences的对象 p ...
- hdu6331 Problem M. Walking Plan
传送门 题目大意 给你一个n点m条边的有向图,q次询问,给定s,t,k,求由s到t至少经过k条边的最短路. 分析 我们设dp[i][j][k]为从i到j至少经过k条边的最短路,sp[i][j]意为从i ...
- 自己实现 String 类
实现Stirng类:构造函数.复制构造.拷贝构造.重载<<符号(友元) #include <iostream> #include <string.h> #inclu ...
- Entity Framework Tutorial Basics(2):What is Entity Framework?
What is Entity Framework? Writing and managing ADO.Net code for data access is a tedious and monoton ...
- SDUT 3402 数据结构实验之排序五:归并求逆序数
数据结构实验之排序五:归并求逆序数 Time Limit: 40MS Memory Limit: 65536KB Submit Statistic Problem Description 对于数列a1 ...
- spark sql建表的异常
在使用spark sql创建表的时候提示如下错误: missing EOF at 'from' near ')' 可以看下你的建表语句中是不是create external table .... ...
- LeetCode第21题:合并两个有序链表
题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1- ...
- WordCount编码与测试
1. github项目地址:https://github.com/wwwwu/WordCount 2.PSP表格: PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning ...