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. openstack修改增加网卡及更改ip

        1.neutron更改及mysql数据库更改网卡及ip:     neutron port-update ae22c84b-22a9-4618-b046-1eb61379bcea  --all ...

  2. ubuntu中安装vsftp

    1.安装 $ sudo apt-get install vsftpd 2. 配置 需要配置文件/etc/vsftpd.conf 来进行设置参数以对FTP进行控制,包括访问权限,流量等进行控制. 执行s ...

  3. WinForm 开发框架 Jade UI Beta

    Jade UI Demo Beta 个人网站:http://www.2to.net 开源地址:https://github.com/dcdlove/JadeUI 预览DEMO下载: http://pa ...

  4. js基础的自定义属性练习

    js基础的自定义属性练习: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type ...

  5. MySQL连表Update修改数据

    设想两张表,如下 table A field id field name table B field id filed my_name 现在希望将表B中的my_name中的内容“拷贝”到表A中对应的n ...

  6. Beta_版本发布

    学号 姓名 201731041215 王阳 201731062302 鲜雨珂 201731062128 邓捷 201731062305 周蓉 201731062131 龙继平 201731062304 ...

  7. bootstrap-table学习

    参考学习  http://bootstrap-table.wenzhixin.net.cn/getting-started/ 包括Bootstrap库(如果你的项目没有使用它)和bootstrap-t ...

  8. 学习Linux的好网站

    http://www.linuxcommand.org/ 很不错的学习shell和script的教程.包含:Learning the shell 和 writing shell scripts 两个内 ...

  9. 新建博客第一天,随意来一发Win8运行命令大全

    1.calc:启动计算器 2.appwiz.cpl:程序和功能   3.certmgr.msc:证书管理实用程序 4.charmap:启动字符映射表 5.chkdsk.exe:Chkdsk磁盘检查(管 ...

  10. BZOJ 2119: 股市的预测 SA

    2119: 股市的预测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 434  Solved: 200[Submit][Status][Discuss ...