HDU 5441——Travel——————【并查集+二分查界限】
Travel
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1313 Accepted Submission(s): 472
For each test case, the first line consists of three integers n,m and q where n≤20000,m≤100000,q≤5000. The Undirected Kingdom has n cities and mbidirectional roads, and there are q queries.
Each of the following m lines consists of three integers a,b and d where a,b∈{1,...,n} and d≤100000. It takes Jack d minutes to travel from city a to city b and vice versa.
Then q lines follow. Each of them is a query consisting of an integer x where x is the time limit before Jack goes berserk.
Note that (a,b) and (b,a) are counted as different pairs and a and b must be different cities.
10000
13000
题目大意:无向图。给你m条边,u - v 每条边有一个权值w。表示从城市u - v、v - u 的时间是w。然后q次询问。每次询问一个x值。表示问你当时间容忍为x时,可以走多少个城市对。每次到一个城市后可以休息,时间容忍会重新变为x。
解题思路:首先按边权把边排序。用并查集统计加入第k条边时可以走的城市对个数,记录在pair_cnt数组中。同时需要维护每个集合中的点的个数。最后在排完序后的边中二分找到第一个大于x的边的编号ck。然后在pair_cnt数组中找到编号ck-1即为答案。
#include<bits/stdc++.h>
using namespace std;
const int maxn=(1e4)*3;
const int maxm=1e5+200;
const int INF=0x3f3f3f3f;
struct Edge{
int u,v,w;
Edge(){}
Edge(int _u,int _v,int _w){
u=_u; v=_v; w=_w;
}
}edges[maxm];
int cnt[maxn],pair_cnt[maxm];
int fa[maxn];
void init(int n){
for(int i=0;i<=n;i++){
fa[i]=i;
cnt[i]=1;
}
}
int Find(int x){
if(fa[x]!=x){
return fa[x]=Find(fa[x]);
}
return x;
}
int Union(int u,int v){
int fu=Find(u);
int fv=Find(v);
int ret;
if(fu<fv){
ret=cnt[fu]*cnt[fv]*2;
fa[fv]=fu;
cnt[fu]+=cnt[fv];
return ret;
}else if(fu>fv){
ret=cnt[fu]*cnt[fv]*2;
fa[fu]=fv;
cnt[fv]+=cnt[fu];
return ret;
}else{
return 0;
}
}
bool cmp(Edge a,Edge b){
return a.w<b.w;
}
int BinSearch(int l,int r,int key){
while(l<r){
int md=(l+r)/2;
if(key<edges[md].w){
r=md;
}else if(key>edges[md].w){
l=md+1;
}else{
return md+1;
}
}
return r;
}
int main(){
int t,n,m,q;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&q);
init(n);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&edges[i].u,&edges[i].v,&edges[i].w);
}
sort(edges+1,edges+1+m,cmp);
edges[0].w=-1;edges[m+1].w=INF;
for(int i=1;i<=m;i++){
pair_cnt[i]=pair_cnt[i-1]+Union(edges[i].u,edges[i].v);
}
int tmp;
for(int i=0;i<q;i++){
scanf("%d",&tmp);
printf("%d\n",pair_cnt[BinSearch(0,m+1,tmp)-1]);
}
}
return 0;
}
HDU 5441——Travel——————【并查集+二分查界限】的更多相关文章
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- hdu 5441 travel 离线+带权并查集
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...
- HDU 3038 How Many Answers Are Wrong (并查集)---并查集看不出来系列-1
Problem Description TT and FF are ... friends. Uh... very very good friends -________-bFF is a bad b ...
- HDU 5441 Travel(并查集+统计节点个数)
http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...
- HDU 5441 Travel (并查集+数学+计数)
题意:给你一个带权的无向图,然后q(q≤5000)次询问,问有多少对城市(城市对(u,v)与(v,u)算不同的城市对,而且u≠v)之间的边的长度不超过d(如果城市u到城市v途经城市w, 那么需要城市u ...
- hdu 4750 Count The Pairs(并查集+二分)
Problem Description With the 60th anniversary celebration of Nanjing University of Science and Techn ...
- hdu 5652 India and China Origins 并查集+二分
India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- Marriage Match II 【HDU - 3081】【并查集+二分答案+最大流】
题目链接 一开始是想不断的把边插进去,然后再去考虑我们每次都加进去边权为1的边,直到跑到第几次就没法继续跑下去的这样的思路,果不其然的T了. 然后,就是想办法咯,就想到了二分答案. 首先,我们一开始处 ...
- 并查集+二分-hdu-4750-Count The Pairs
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4750 题目大意: 给一无向图,n个点,m条边,每条边有个长度,且不一样.定义f(i,j)表示从节点i ...
随机推荐
- 理解JavaScript普通函数以及箭头函数里使用的this
this 普通函数的this 普通函数的this是由动态作用域决定,它总指向于它的直接调用者.具体可以分为以下四项: this总是指向它的直接调用者, 例如 obj.func() ,那么func()里 ...
- Web Server Jexus配置及使用
Web Server Jexus配置及使用 一.jexus概念: Jexus 即 Jexus Web Server,简称JWS,是Linux平台上的一款ASP.NET WEB服务器,是 Linux. ...
- Spring Boot 学习系列(05)—自定义视图解析规则
此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 自定义视图解析 在默认情况下Spring Boot 的MVC框架使用的视图解析ViewResolver类是C ...
- ubuntu - 14.04,必须会的技能-安装PPA源中的程序,更大范围使用deb格式安装文件!!
在使用ubuntu时候,管理各种软件最方便的方式肯定是使用软件中心了,这个管理工具类似windows的 程序管理了,使用它有两个好处: 1,无需处理包依赖,linux里面程序存在各种依赖关系,这在以往 ...
- UIViewController函数调用顺序
/*********** 0 执行1次而已 ******************/ + (void)load { NSLog(@" 0:%s", __func__); } /*** ...
- 常用SQL语句及在node中使用MySQL
摘要:一些重要的SQL命令 SELECT - 从数据库中提取数据 UPDATE - 更新数据库中的数据 DELETE - 从数据库中删除数据 INSERT INTO - 向数据库中插入新数据 CREA ...
- typeof 和 instanceof
typeof 和 instanceof 都是用来判断类型的函数 typeof 对于原始类型来说,除了 null 都可以显示正确的类型 typeof 1 // 'number' typeof '1' / ...
- kuangbin专题七 HDU1754 I Hate It (单点修改维护最大值)
很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有 ...
- zabbix+telegram的API接口(告警)
首先在telegram里创建一个有API接口的用户,创建是在 @BotFather 选择/start——————/newbot 输入机器人的用户名,根据提示操作.获得bot的API接口和群ID 通 ...
- linux上传与下载
首先必须安装xshell这个工具 使用xshell来操作服务非常方便,传文件也比较方便.就是使用rz,sz首先,服务器要安装了rz,szyum install lrzsz当然你的本地windows主机 ...