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 ;
}

最新文章

  1. python远程连接paramiko 模块和堡垒机实现
  2. dhtmlx相关
  3. 数据库MySQL开篇
  4. 初学UML——用例图
  5. [Effective JavaScript 笔记] 第11条:熟练掌握闭包
  6. Hadoop:使用原生python编写MapReduce
  7. SVN客户端解决authorization failed问题
  8. 关于androidManifest.xml的概叙以及intent-filter的详细分析
  9. UITableViewCell 高度自适应
  10. uniq linux下去除重复行命令
  11. Mybatis 学习历程
  12. TCP/IP详解之:广播和多播
  13. javascript 限制字符串字数换行 带BUG
  14. Java的动态加载及其安全性问题
  15. iis7.0 ExtensionlessUrlHandler-Integrated-4.0解决方法
  16. [GitHub]第四讲:合并分支
  17. js 判断js,css是否引入,确保不重复引入
  18. 分布式计算课程补充笔记 part 1
  19. react中组件的渲染
  20. 解题:国家集训队 Middle

热门文章

  1. 安卓linux真机调试
  2. leetcode542 01 Matrix
  3. How to install Eclipse in linux
  4. codevs 2915 期末考试
  5. webpack打包性能分析
  6. 第2节 azkaban调度:1、azkaban的调度任务使用
  7. tomcat中如何禁止和允许主机或地址访问
  8. PHP 腾讯云cos使用之我见
  9. 电商技术中企业数据总线ESB和注册服务管理的区别
  10. Clover KextsToPatch 使用方法 2015.10.21