Travel

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 4685    Accepted Submission(s): 1535

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≤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 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.

 
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
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  6361 6360 6359 6358 6357 
 
 
 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
#define N 100009
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a))
int t,n,m,q;
struct Node{
int u,v,w;
}nod[N];
struct Nod{
int id,w;
}no[N];
int pre[];
int cnt[];
bool cmp(Node a,Node b){
return a.w<b.w;
}
bool cmp1(Nod a,Nod b){
return a.w<b.w;
}
void init()
{
for(int i=;i<=n;i++){
pre[i]=i;
cnt[i]=;
}
}
int find(int x)
{
return pre[x]=x==pre[x]?x:find(pre[x]);
}
void unite(int u,int v)
{ /*
pre[v]=u;
cnt[u]+=cnt[v];
*/
pre[u]=v;
cnt[v]+=cnt[u];
//上面两种写法都是对的。
}
int ret[N];
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&q);
init();
for(int i=;i<m;i++){
scanf("%d%d%d",&nod[i].u,&nod[i].v,&nod[i].w);
}
sort(nod,nod+m,cmp);
for(int i=;i<q;i++){
scanf("%d",&no[i].w);
no[i].id=i;
}
sort(no,no+q,cmp1);
int j=,ans=;
int u,v;
//预处理
for(int i=;i<q;i++){
while(j<m&&nod[j].w<=no[i].w){
u=find(nod[j].u);
v=find(nod[j].v);
if(u!=v){
int tmp=cnt[v]+cnt[u];
//任意一对点都行
ans+=(tmp*(tmp-))-(cnt[u]*(cnt[u]-))-(cnt[v]*(cnt[v]-));
//减去已经加过的
unite(u,v);
}
j++;
}
ret[no[i].id]=ans;
}
for(int i=;i<q;i++){
printf("%d\n",ret[i]);
}
}
return ;
}

hdu 5441的更多相关文章

  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. (并查集)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 ...

  5. 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 ...

  6. HDU 5441 离线处理 + 并查集

    题意:给n个节点m条带权值边的无向图.然后q个问题,每次询问点对的数目,点对需要满足的条件是:1)连通:2)其路径的最大权值不能超过询问值. 分析:如果没次询问一次,dfs一次,很可能超时,因此可以用 ...

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

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

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

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

  9. Travel(HDU 5441 2015长春区域赛 带权并查集)

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

  10. hdu 5441 Travel(并查集)

    Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...

随机推荐

  1. Dell服务器安装系统中遇到的坑

    在本学期开学初期,由于后续实验的需要,老师为我们配置了服务器,该服务器的型号为Dell Power R730. 由于我也是一个小白,在服务器安装系统的过程中,遇到了一些麻烦,在这里记录下来,希望自己能 ...

  2. Win7环境下配置FTP

    1.打开 控制面板-->程序和功能-->打开或关闭Windows资源,在弹出的窗体里找到 “Internet信息服务”,展开后选择“Ftp服务器",然后点击"确定&qu ...

  3. 192.168.28.168:3000打开网页无法调试localhost为前缀的接口

    最近我在IIS上发布.net Core API的时候发现 当我用localhost去打开我的web项目时 并且,ajax调用的接口前缀为localhost时候,运行正确 但是当我用192.168.28 ...

  4. Linux常用操作详解

    第1章 Linux命令基础 1.1 习惯 操作前备份,操作后检查 1.2 简单目录结构 一切从根开始,与windows不同 1.3 规则 [root@znix ~]# [用户名@主机名 你在哪]# 1 ...

  5. Android 4.4及以后将内容布局延伸到状态栏

    首先说明:该文章不是大家说的沉浸式状态栏,网上沉浸式状态栏的博客很多,搜索就有了! 该篇博客的主要目的就是为了将图片显示在状态栏上,让APP看起来更有型!如下图所示:   界面 这个界面的布局就是co ...

  6. Eucalyptus——EC2的开源实现(转载)

    Eucalyptus[22]是加利福尼亚大学的 Daniel Nurmi 等人实现的,是一个用于实现云计算的开源软件基础设施.Eucalyptus 是 Amazon EC2 的一个开源实现,它与 EC ...

  7. 查询日志logcat使用总结

    cmd命令行中使用adb logcat命令查看Android系统和应用的log,dos窗口按ctrl+c中断输出log记录.logcat日志中的优先级/tag标记: android输出的每一条日志都有 ...

  8. pc端常见布局---垂直居中布局 单元素不定高

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 使用脚本在Linux服务器上自动安装Kubernetes的包管理器Helm

    Helm之于Kubernetes好比yum之于Red Hat Enterprise Linux,或者apt-get之于Ubuntu. Helm是由helm CLI和Tiller组成,是典型的Clien ...

  10. ABAP Development Tools的语法高亮实现原理

    ABAP Development Tools的前端是Java,根本识别不了ABAP.那么在ADT里的ABAP语法高亮是如何实现的? 第一次打开一个report时,显示在ADT里的代码是没有任何语法高亮 ...