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. TCP简单程序

    服务器段: package com.dcz.socket; import java.io.IOException; import java.io.OutputStream; import java.n ...

  2. Windows 8.1 explorer.exe 出错 “Application Hang”

    不知道为什么explorer常常会卡一下 看系统日志发现有来源于“Application Hang”的错误 部分常规信息: 程序 explorer.exe 版本 6.3.9600.17415 停止与 ...

  3. java 核心技术卷一笔记 6 .1接口 lambda 表达式 内部类

    6.1 接口不是类,是对类的一组需求的描述,这些类需要遵守接口描述的统一格式进行定义.例如:Arrays类中sort方法(可以对对象数组进行排序)前提是对象所属的类必须实现了Comparable 接口 ...

  4. js 前端不调接口直接下载图片

    // 下载图片 downPhoto (path) { this.downloadFiles(path) }, // 下载 downloadFiles (content) { console.log(c ...

  5. Java Web应用中获取用户请求相关信息,如:IP地址、操作系统、浏览器等信息

    引入jar包 <dependency> <groupId>eu.bitwalker</groupId> <artifactId>UserAgentUti ...

  6. PE基础2

    PE课程002 怎么找到Nt头? (PIMAGE_NT_HEADER)(DOS.e_lfanew + (DWORD)m_pBuff) 怎么找到第一个区段表? 区段头位置 = pNt + 4 + 文件头的 ...

  7. 阿里云apt-get安装包时Err:2 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/main amd64 git amd64 1:2.7.4-0ubuntu1.2 404 Not Found

    新部署的云服务器出现如下错误: root@iZj6cbjalvhsw0fhndmm5xZ:~# apt-get install git Reading package lists... Done Bu ...

  8. 第1节 flume:15、flume案例二,通过自定义拦截器实现数据的脱敏

    1.7.flume案例二 案例需求: 在数据采集之后,通过flume的拦截器,实现不需要的数据过滤掉,并将指定的第一个字段进行加密,加密之后再往hdfs上面保存 原始数据与处理之后的数据对比 图一  ...

  9. Android之通过adb shell 模拟器 error: more than one device and emulator 改ip dns

    error: more than one device and emulator 如果出现上面那种情况 请关闭  ide 输入下面的  再次重新启动 模拟器 如果实际上只有一个设备或模拟器,并且查到有 ...

  10. xheditor的参数配置详解

    2.2. 初始化参数列表 2.3. API函数接口列表 2.4. 上传程序开发规范 2.5. 插件开发指南 2.6. 皮肤设计指南 2.2. 初始化参数列表 初始化参数示例代码: $('#elm1') ...