Travel

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1313    Accepted Submission(s): 472

Problem Description
Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are n cities and m bidirectional roads connecting the cities. Jack hates waiting too long on the bus, but he can rest at every city. Jack can only stand staying on the bus for a limited time and will go berserk after that. Assuming you know the time it takes to go from one city to another and that the time Jack can stand staying on a bus is x minutes, how many pairs of city (a,b) are there that Jack can travel from city a to b without going berserk?
 
Input
The first line contains one integer T,T≤5, which represents the number of test case.

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.

 
Output
You should print q lines for each test case. Each of them contains one integer as the number of pair of cities (a,b) which Jack may travel from a to b within the time limit x.

Note that (a,b) and (b,a) are counted as different pairs and a and b must be different cities.

 
Sample Input
1
5 5 3
2 3 6334
1 5 15724
3 5 5705
4 3 12382
1 3 21726
6000
10000
13000
 
Sample Output
2
6
12
 
Source

题目大意:无向图。给你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——————【并查集+二分查界限】的更多相关文章

  1. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  2. hdu 5441 travel 离线+带权并查集

    Time Limit: 1500/1000 MS (Java/Others)  Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...

  3. 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 ...

  4. HDU 5441 Travel(并查集+统计节点个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...

  5. HDU 5441 Travel (并查集+数学+计数)

    题意:给你一个带权的无向图,然后q(q≤5000)次询问,问有多少对城市(城市对(u,v)与(v,u)算不同的城市对,而且u≠v)之间的边的长度不超过d(如果城市u到城市v途经城市w, 那么需要城市u ...

  6. hdu 4750 Count The Pairs(并查集+二分)

    Problem Description With the 60th anniversary celebration of Nanjing University of Science and Techn ...

  7. hdu 5652 India and China Origins 并查集+二分

    India and China Origins Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  8. Marriage Match II 【HDU - 3081】【并查集+二分答案+最大流】

    题目链接 一开始是想不断的把边插进去,然后再去考虑我们每次都加进去边权为1的边,直到跑到第几次就没法继续跑下去的这样的思路,果不其然的T了. 然后,就是想办法咯,就想到了二分答案. 首先,我们一开始处 ...

  9. 并查集+二分-hdu-4750-Count The Pairs

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4750 题目大意: 给一无向图,n个点,m条边,每条边有个长度,且不一样.定义f(i,j)表示从节点i ...

随机推荐

  1. Binder学习笔记(十二)—— binder_transaction(...)都干了什么?

    binder_open(...)都干了什么? 在回答binder_transaction(...)之前,还有一些基础设施要去探究,比如binder_open(...),binder_mmap(...) ...

  2. [BZOJ4521][Cqoi2016]手机号码 (数位dp)

    题目描述 人们选择手机号码时都希望号码好记.吉利.比如号码中含有几位相邻的相同数字.不含谐音不吉利的数字等.手机运营商在发行新号码时也会考虑这些因素,从号段中选取含有某些特征的号码单独出售.为了便于前 ...

  3. iOS自定义相机

    1.首先声明以下对象 #import <AVFoundation/AVFoundation.h> //捕获设备,通常是前置摄像头,后置摄像头,麦克风(音频输入) @property (no ...

  4. (Python OpenGL)【 0】关于VAO和VBO以及OpenGL新特性

    (Python OpenGL)关于新版OpenGL需要了解的: 随着OpenGL状态和固定管线模式的移除,我们不在用任何glEnable函数调用,而且也不会有glVertex.glColor等函数调用 ...

  5. Flume启动时报错Caused by: java.lang.InterruptedException: Timed out before HDFS call was made. Your hdfs.callTimeout might be set too low or HDFS calls are taking too long.解决办法(图文详解)

    前期博客 Flume自定义拦截器(Interceptors)或自带拦截器时的一些经验技巧总结(图文详解) 问题详情 -- ::, (agent-shutdown-hook) [INFO - org.a ...

  6. HDU6301 Distinct Values (多校第一场1004) (贪心)

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. Qt 学习之路 2(4):信号槽

    Home / Qt 学习之路 2 / Qt 学习之路 2(4):信号槽 Qt 学习之路 2(4):信号槽  豆子  2012年8月23日  Qt 学习之路 2  110条评论 信号槽是 Qt 框架引以 ...

  8. ubuntu中出现:程序 'java' 已包含在下列软件包中的解决方法

    已经安装sun java 在终端中输入java,出现以下提示: 程序 'java' 已包含在下列软件包中: * default-jre * gcj-4.8-jre-headless * gcj-4.9 ...

  9. C语言单片机中延时程序的实现

    在单片机或嵌入式系统的程序,常常用规定次数的空循环来实现延时 /** * 通过一个空循环体循环让程序运行一段时间.在嵌入式系统中,这个函数用来实现延时. * * 参数: *    u16 i -- 循 ...

  10. C语言中typedef的解释_1

    typedef是在计算机编程语言中用来为复杂的声明定义简单的别名,它与宏定义有些差异. 它本身是一种存储类的关键字,与auto.extern.mutable.static.register等关键字不能 ...