hdu 5441 Travel 离线带权并查集
Travel
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=5441
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 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≤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
HINT
题意
给你一个图,无向边。
有5000个询问
每次询问,问你有多少对城市之间的最长度不超过D
题解:
离线处理,离线之后用并查集维护就好了
按照边权排序,每次就把小于d还没有添加过的边连进去就好了
每次都由并查集维护 ,带权并查集
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
#include <bitset>
typedef unsigned char byte;
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define pi acos(-1) using namespace std;
const int maxn = 2e4 + ;
int set[maxn] , n , m , q , ans[maxn] , number[maxn] , cot = ; struct Edge
{
int u , v , w;
Edge(int u ,int v ,int w) : u(u) , v(v) , w(w) {}
friend bool operator < (const Edge & x,const Edge & y)
{
return x.w < y.w;
}
}; struct Query
{
int val , idx;
friend bool operator < (const Query & x,const Query & y)
{
return x.val < y.val;
}
}; vector<Edge>e;
Query qq[]; int find_set(int u)
{
return set[u] ^ u ? set[u] = find_set(set[u]) : u;
} int union_set(int u,int v)
{
int p1 = find_set(u);
int p2 = find_set(v);
if(p1 != p2)
{
int x = number[p1];
int y = number[p2];
cot -= x*(x-);
cot -= y*(y-);
number[p2] += number[p1];
cot += (number[p2])*(number[p2]-);
number[p1] = ;
set[p1] = p2;
}
} void initiation()
{
scanf("%d%d%d",&n,&m,&q);
for(int i = ; i <= n ; ++ i)
{
set[i] = i;
number[i] = ;
}
for(int i = ; i <= m ; ++ i)
{
int u , v , w;
scanf("%d%d%d",&u,&v,&w);
e.push_back(Edge(u,v,w));
}
sort(e.begin(),e.end());
for(int i = ; i <= q ; ++ i)
{
int d;
scanf("%d",&d);
qq[i].val = d , qq[i].idx = i;
}
sort( qq + , qq + + q);
} void solve()
{
int ptr = ;
for(int i = ; i <= q; ++ i)
{
while(ptr < m && e[ptr].w <= qq[i].val)
{
union_set(e[ptr].u,e[ptr].v);
ptr++;
}
ans[qq[i].idx] = cot;
}
for(int i = ; i <= q; ++ i) printf("%d\n",ans[i]);
} void clear_all()
{
e.clear();
cot = ;
} int main(int argc,char *argv[])
{
int Case;
scanf("%d",&Case);
while(Case--)
{
initiation();
solve();
clear_all();
}
return ;
}
hdu 5441 Travel 离线带权并查集的更多相关文章
- hdu 5441 travel 离线+带权并查集
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...
- Hdu 2047 Zjnu Stadium(带权并查集)
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- How Many Answers Are Wrong (HDU - 3038)(带权并查集)
题目链接 并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的. 思考: 1.如何建立区间之间的联系 2.如何发现悖论 首先是如何 ...
- hdu 2818 Building Block (带权并查集,很优美的题目)
Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...
- hdu 3635 Dragon Balls (带权并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- How Many Answers Are Wrong HDU - 3038 (经典带权并查集)
题目大意:有一个区间,长度为n,然后跟着m个子区间,每个字区间的格式为x,y,z表示[x,y]的和为z.如果当前区间和与前面的区间和发生冲突,当前区间和会被判错,问:有多少个区间和会被判错. 题解:x ...
- hdu 3047–Zjnu Stadium(带权并查集)
题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这m个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突. 分析: 首 ...
- HDU 3047 Zjnu Stadium(带权并查集)
题意:有一个环形体育场,有n个人坐,给出m个位置关系,A B x表示B所在的列在A的顺时针方向的第x个,在哪一行无所谓,因为假设行有无穷个. 给出的座位安排中可能有与前面矛盾的,求有矛盾冲突的个数. ...
- POJ 1984 Navigation Nightmare 【经典带权并查集】
任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K To ...
随机推荐
- bzoj1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
筛法. 枚举每个数,它会对它的倍数的答案有贡献. 数大了以后,倍数相应少了很多.比枚举每个数的约数要好的多. 自己yy了一种分步做法.小于sqrt(m)被当作约数枚举,大于sqrt(m)的枚举倍数. ...
- bzoj3940: [Usaco2015 Feb]Censoring
AC自动机.为什么洛谷水题赛会出现这种题然而并不会那么题意就不说啦 .终于会写AC自动机判断是否是子串啦...用到kmp的就可以用AC自动机水过去啦 #include<cstdio> #i ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- NET下RabbitMQ实践[示例篇]
在上一篇文章中,介绍了在window环境下安装erlang,rabbitmq-server,以免配置用户,权限,虚拟机等内容. 今天将会介绍如果使用rabbitmq进行简单的消息入队, ...
- [转] “无法注册程序集***dll- 拒绝访问。请确保您正在以管理员身份运行应用程序。对注册表项”***“的访问被拒绝
原文 Win8下Visual Studio编译报“无法注册程序集***dll- 拒绝访问.请确保您正在以管理员身份运行应用程序.对注册表项”***“的访问被拒绝.”问题修正 原来在Win7下Visua ...
- 让你的.NET程序支持多语言
辛辛苦苦做出来的软件,我们当然希望能让更多的人用,支持多语言是必须的.下面我将以Asp.net Web Form为例来介绍如何支持多语言.其他程序比如windows程序,过程都是大同小异的. 大概分以 ...
- <转>MySql 与Oracle区别
http://blog.sina.com.cn/s/blog_61e034d50100k6xn.html 近期突击学习了mysql,应杨毅的邀请,简单比较一下mysql和oracle的差别,不当之处欢 ...
- android NDK 实用学习(三)- java端类对象的构造及使用
1,读此文章前我假设你已经读过: android NDK 实用学习-获取java端类及其类变量 android NDK 实用学习-java端对象成员赋值和获取对象成员值 2,java端类对象的构造: ...
- ffmpeg ffprobe ffplay
./ffprobe -print_format json -show_format -show_frames -select_streams v -i ../hhh.flv ./ffprobe -pr ...
- 11个高级MySQL数据库面试问题和答案
因为有大家的支持,我们才能做到现在,感谢你们这一路上对我们的支持.在这篇文章中,我们将主要针对MySQL的实用技巧,讲讲面试中相关的问题. 1. 如何使用SELECT语句找到你正在运行的服务器的版本并 ...