Problem 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≤, which represents the number of test case.

For each test case, the first line consists of three integers n,m and q where n≤,m≤,q≤. 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∈{,...,n} and d≤. 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

 
Sample Output

 
Source
 

比赛的时候坑在这题了,一直TLE,统计个数的方法不对,导致一直在这题徘徊,否则就能去思考其它的题了,可能就不是这种破成绩了。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 600000
#define inf 1e12
int n,m,q;
struct Node{
int u,v,d;
}edge[N];
struct Node1{
int num;
int idd;
int ans;
}query[];
bool cmp1(Node a,Node b){
return a.d<b.d;
}
bool cmp2(Node1 a,Node1 b){
return a.num<b.num;
}
bool cmp3(Node1 a,Node1 b){
return a.idd<b.idd;
}
/////////////////////////////////////////////
int fa[];
int cnt[];
void init(){
for(int i=;i<;i++){
fa[i]=i;
cnt[i]=;
}
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
} ////////////////////////////////////////////////////// int main()
{
int t;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<m;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].d);
}
for(int i=;i<q;i++){
scanf("%d",&query[i].num);
query[i].idd=i;
query[i].ans=;
}
sort(edge,edge+m,cmp1);
sort(query,query+q,cmp2);
int w=;
int ans=;
for(int i=;i<q;i++){
//printf("+=====");
while(w<m && edge[w].d<=query[i].num){ int root1=find(edge[w].u);
int root2=find(edge[w].v); if(root1!=root2){
ans+=cnt[root1]*cnt[root2]; //printf("***%d\n",ans);
cnt[root2]+=cnt[root1];
fa[root1]=root2;
}
w++;
}
//printf("%d\n",ans*2);
query[i].ans=ans*;
}
sort(query,query+q,cmp3);
for(int i=;i<q;i++){
printf("%d\n",query[i].ans);
}
}
return ;
}

hdu 5441 Travel(并查集)的更多相关文章

  1. 2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v ...

  2. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  3. HDU 5441 Travel(并查集+统计节点个数)

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

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

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

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

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

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

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

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

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

  8. HDU 2818 (矢量并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...

  9. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

随机推荐

  1. ios想要取消执行延时调用的方法

    想要取消执行延时调用的方法: [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideDia ...

  2. UILabel+Create

    #import <UIKit/UIKit.h> @interface UILabel (Create) /** * 创建普通Label * * @param frame frame * @ ...

  3. hdu 5063 Operation the Sequence(Bestcoder Round #13)

    Operation the Sequence                                                                     Time Limi ...

  4. Apache与tomcat

    联系 1)Apache和tomcat都是web网络服务器 2)Apache是普通的服务器,本身支持html即普通网页,可以通过插件支持php也可以与Tomcat连通  (Apache单向连接tomca ...

  5. ASP.NET母版与内容页相对路径的问题

    1. 图片问题 非常好解决 <img runat="server" src="~/images/ad468x60.gif" alt="" ...

  6. FullCalendar 的学习笔记(一)

    前一段时间,一个老项目需要新增一个小功能,日程表~ 于是网上找了下,发现FullCalendar这个控件还不错于是就拿来用了下,下面简单介绍下我的学习笔记. 首先就是了解下FullCalendar的A ...

  7. JSP基础学习(一)

    1.jsp和servlet是javaEE规范的两个基本成员,是java web开发的重要知识,jsp和servlet本质上是一样的,因此jsp最终必须编译成servlet才能运行,或者说jsp是生成s ...

  8. Python学习笔记6(列表生成式)

    1.生成列表 要生成list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],我们可以用range(1, 11): >>> range(1, 11) [1, 2, 3 ...

  9. c++连接mysql数据库(使用mysql api方式,环境VS2013+MYSQL5.6)

    转载请注明出处,原文地址http://www.cnblogs.com/zenki-kong/p/4382657.html 刚开始写博客,博主还只是个大三汪,学艺不精,如有错误还请前辈指出(>^ω ...

  10. centos 6.4从源码安装mysql 5.6笔记

    上周在安装mysql时遇到了些许麻烦,今天整理下. 在代码目录建立obj文件夹. 在obj目录下,执行cmake .. -DXXX  // XXX表示一些参数,详见http://dev.mysql.c ...