Travel

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

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 \leq 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 \leq 20000, m \leq 100000, q \leq 5000$. The Undirected Kingdom has $n$ cities and $m$ bidirectional 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 \leq 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
 
解题:带权并查集 + 离线处理
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
using LL = long long;
int uf[maxn];
LL ret,ans[maxn],cnt[maxn];
struct arc {
int u,v,w;
bool operator<(const arc &rhs) const {
return w < rhs.w;
}
} e[];
struct QU {
int w,id;
bool operator<(const QU &rhs) const{
return w < rhs.w;
}
} Q[maxn];
int Find(int x) {
if(x != uf[x]) uf[x] = Find(uf[x]);
return uf[x];
}
bool Union(int x,int y) {
x = Find(x);
y = Find(y);
if(x == y) return false;
ret -= cnt[x]*(cnt[x] - ) + cnt[y]*(cnt[y] - );
ret += (cnt[x] + cnt[y])*(cnt[x] + cnt[y] - );
if(cnt[x] < cnt[y]) {
uf[x] = y;
cnt[y] += cnt[x];
cnt[x] = ;
} else {
uf[y] = x;
cnt[x] += cnt[y];
cnt[y] = ;
}
return true;
}
int main() {
int kase,n,m,q;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d%d",&n,&m,&q);
for(int i = ; i < m; ++i)
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
for(int i = ; i < q; ++i) {
scanf("%d",&Q[i].w);
Q[i].id = i;
}
sort(e,e + m);
sort(Q,Q + q);
for(int i = ; i <= n; ++i) {
uf[i] = i;
cnt[i] = ;
}
ret = ;
for(int i = ,j = ; i < q; ++i) {
while(j < m && e[j].w <= Q[i].w) {
Union(e[j].u,e[j].v);
++j;
}
ans[Q[i].id] = ret;
}
for(int i = ; i < q; ++i)
printf("%I64d\n",ans[i]);
}
return ;
}

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(并查集+统计节点个数)

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

  3. hdu 5441 Travel (2015长春网赛)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题目大意是给一个n个城市(点)m条路线(边)的双向的路线图,每条路线有时间值(带权图),然后q个询问,每个 ...

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

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

  5. hdu 5441 Travel(并查集)

    Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...

  6. HDU 5441——Travel——————【并查集+二分查界限】

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

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

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

  8. HDU 5441 Travel (离线dsu)

    <题目链接> 题目大意:$n$个点,$m$条边,每条边具有对应的权值,然后进行$k$次询问,每次询问给定一个值,所有权值小于等于这个的边所对应的点能够相连,问每次询问,这些能够相互到达的点 ...

  9. (并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others)    Memo ...

随机推荐

  1. 本机运行x程序出现:Can't open display 原因及其解决方法(貌似非永久)

    http://blog.sina.com.cn/s/blog_53db572501016ma7.html 这是因为Xserver默认情况下不允许别的用户的图形程序的图形显示在当前屏幕上. 如果需要别的 ...

  2. spring mvc添加静态资源访问时@Controller无效的解决

    web.xml中的url-pattern设置为/,添加mvc:resources访问静态资源时,@Controller无效的问题 web.xml: <servlet> <servle ...

  3. hybrid app开发中:苹果移动设备实用Meta标签

    hybrid app开发中:苹果移动设备实用Meta标签 “apple-mobile-web-app-status-bar-style”作用是控制状态栏显示样式 具体效果如下: status-bar- ...

  4. 快速搭建一个Fabric 1.0的环境(转)

    文章来源:http://www.cnblogs.com/studyzy/p/7437157.html 感谢博主@深蓝居 提供的技术文档,按照文档根据自己遇到的问题做了一定修改,方便自己回顾和再次搭建 ...

  5. 爬虫requests库的基本用法

    需要注意的几个点: 1.后面的s是一个虚拟目录 2.url后面不用加问号,发起请求的时候会自动帮你加上问号 get_url = 'http://www.baidu.com/s' 3. url的特性:u ...

  6. 国内的Jquery CDN免费服务

    Jquery是个非常流行的JS前端框架,在很多网站都能看到它的身影.很多网站都喜欢采用一些Jquery CDN加速服务,这样网站加载jquery会更快.之前火端网络的一些网站都是使用Google的jq ...

  7. Genymotion的安装与设置

    Genymotion是一款非常好用的虚拟机,利用它可以在window.Liunx或MAC系统上实现Android的模似器.对于开发人员来说,有了Android模似器,就可以在电脑上实时调试安卓app, ...

  8. React Native 手工搭建环境 之iOS篇

    常识 React native 开发服务器 在开发时,我们的框架是这样的:  当正式发布进入到生产环境时,开发服务器上所有的js文件将会被编译成包的形式,直接嵌入到客户端内.这时,已经不再需要开发服 ...

  9. iOS图片目录批量复制到android图片目录

    复制shell脚本 #!/bin/bash for i in `ls` do for imgname in `ls $i | grep '^WM.*'` do echo $imgname cp $i/ ...

  10. Python进程间通信和网络基础

    Python进程间通信和网络基础 Python支持多种进程间通讯的方式, 有单机通信的signal和mmap等, 也有可以通过网络的socket方式, 这里先介绍select等的有关知识, socke ...