hdu 5441 travel 离线+带权并查集
Time Limit: 1500/1000 MS (Java/Others)
Memory Limit: 131072/131072 K (Java/Others)
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.
Note that (a,b) and (b,a) are counted as different pairs and a and b must be different cities.
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<set>
- using namespace std;
- struct Edge
- {
- int u,v,w;
- };
- Edge edge[+];
- struct Query
- {
- int x,id;
- };
- Query qry[];
- int w[+];
- long long print[];
- int fa[+];
- void init(int n)
- {
- for(int i=;i<=n;i++){
- fa[i]=i;
- w[i]=;
- }
- }
- bool cmp1(Edge z,Edge y)
- {
- return z.w<y.w;
- }
- bool cmp2(Query z,Query y)
- {
- return z.x<y.x;
- }
- int find_fa(int a)
- {
- if(fa[a]==a)
- return a;
- else{
- fa[a]=find_fa(fa[a]);
- w[fa[a]]+=w[a];
- w[a]=;
- return fa[a];
- }
- }
- void solve(int ,int ,int );
- int main()
- {
- int test;
- scanf("%d",&test);
- while(test--){
- int n,m,Q;
- scanf("%d %d %d",&n,&m,&Q);
- init(n);
- for(int i=;i<=m;i++){
- scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].w);
- }
- for(int i=;i<=Q;i++){
- scanf("%d",&qry[i].x);
- qry[i].id=i;
- }
- solve(n,m,Q);
- }
- return ;
- }
- void solve(int n,int m,int Q)
- {
- sort(edge+,edge+m+,cmp1);
- sort(qry+,qry+Q+,cmp2);
- int cnt=;
- long long sum=;
- for(int i=;i<=Q;i++){
- while(cnt<=m&&edge[cnt].w<=qry[i].x){
- int fau=find_fa(edge[cnt].u);
- int fav=find_fa(edge[cnt].v);
- if(fau!=fav){
- sum+=w[fau]*w[fav]*;
- fa[fau]=fav;
- w[fav]+=w[fau];
- w[fau]=;
- }
- cnt++;
- }
- print[qry[i].id]=sum;
- }
- for(int i=;i<=Q;i++){
- printf("%I64d\n",print[i]);
- }
- return ;
- }
hdu 5441 travel 离线+带权并查集的更多相关文章
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- 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 ...
随机推荐
- hihoCoder挑战赛23
hihoCoder挑战赛23 A.Emulator 题意 给一张图,有\(N(N \le 300)\)个点, 给出任意两点之间的最短路. 求最多可以去掉多少条边,使得任意两点的最短路长度不变. 思路 ...
- java基础之:匿名内部类
在java提高篇-----详解内部类中对匿名内部类做了一个简单的介绍,但是内部类还存在很多其他细节问题,所以就衍生出这篇博客.在这篇博客中你可以 了解到匿名内部类的使用.匿名内部类要注意的事项.如何初 ...
- C++的异常处理
一.什么是异常处理 一句话:异常处理就是处理程序中的错误. 二.为什么需要异常处理,以及异常处理的基本思想 C++之父Bjarne Stroustrup在<The C++ Programming ...
- 初识boost之boost::share_ptr用法
boost中提供了几种智能指针方法:scoped_ptr shared_ptr intrusive_ptr weak_ptr,而标准库中提供的智能指针为auto_ptr. 这其中,我最喜欢,使用最多的 ...
- Instructions函数对照表:02 xmmintrin.h与SSE指令集[转]
更多详情见——http://www.cnblogs.com/zyl910/archive/2012/04/26/md00.htmlSIMD函数整理:00 索引贴 R:寄存器.M:64位MM寄存器:X: ...
- glibc下的内存管理
在解码过程中我们也遇到了类似的问题,第一次解码的音频比较大60s,耗了3G的内存,reset之后内存并没有退还给操作系统,第二次即使解一个10s的音频 几周前我曾提到,我被项目组分配去做了一些探究li ...
- Concurrent inserts on MyISAM and the binary log
Recently I had an interesting surprise with concurrent inserts into a MyISAM table. The inserts were ...
- expect安装去测试
1.下载expect和tcl 下载地址:http://download.csdn.net/download/tobyaries/5754943 2.安装expect tar -zxvf tcl8.4. ...
- Linux静态库和共享库
1.什么是静态库静态库类似windows中的静态lib 关于windows中的静态lib,可参考 Windows动态链接库DLL 特点:包含函数代码声明和实现,链接后所有代码都嵌入到宿主程序中. 只在 ...
- mvn使用问题
http://mirrors.ibiblio.org/maven2/org/apache/maven/archetypes/ http://blog.csdn.net/u011340807/artic ...