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. Mysql一个表编码的坑,mark一下

    问题:一个sql执行很慢,5分钟左右,关键是最大的表是5万出头,另一张表不到5000原因:是两个表的字符集不同,导致匹配时,没有匹配到 解决办法:将两个表的字符集改成一样具体的命令: ALTER TA ...

  2. MVC FileResult

    你如何将文件传送给用户取决于你最开始如何存储它,如果你将文件存入数据库,你会用流的方式将文件返还给用户,如果你将文件存在硬盘中,你只需要提供一个超链接即可,或者也可以以流的方式.每当你需要以流的方式将 ...

  3. 电脑Bois中usb模式启动热键

    组装机主板 品牌笔记本 品牌台式机 主板品牌 启动按键 笔记本品牌 启动按键 台式机品牌 启动按键 华硕主板 F8 联想笔记本 F12 联想台式机 F12 技嘉主板 F12 宏基笔记本 F12 惠普台 ...

  4. MariaDB 实现主从复制

    實驗目的: MariaDB為MySQL的一個分支,其完全開源.無版權之虞且操作上與 MySQL 一脈相承,實際應用中非常廣泛,軟件本身很小,安裝容易,使用簡單. 但其也有缺點,指令行方式操作,無原生G ...

  5. DOCKER启动失败Job for docker.service failed because the control process exited with error code. See "syste mctl status docker.service" and "journalctl -xe" for details.

    [root@localhost ~]# systemctl start docker Job for docker.service failed because the control process ...

  6. mysql(数据库,sql语句,普通查询)

    第1章 数据库 1.1 数据库概述 l 什么是数据库 数据库就是存储数据的仓库,其本质是一个文件系统,数据按照特定的格式将数据存储起来,用户可以对数据库中的数据进行增加,修改,删除及查询操作. l 什 ...

  7. dubbo服务降级(2)

    dubbo降级服务 使用dubbo在进行服务调用时,可能由于各种原因(服务器宕机/网络超时/并发数太高等),调用中就会出现RpcException,调用失败. 服务降级就是指在由于非业务异常导致的服务 ...

  8. 【虚拟机-网关】如何在使用应用程序网关和 Nginx 的环境下实现强制 HTTPS 跳转

    背景介绍 大家在使用 Nginx 部署网站时,实现 HTTP 到 HTTPS 的强制跳转是非常容易的事情,一般可以使用rewrite 命令或者使用返回自定义 301 页面的方法对 HTTP 请求进行 ...

  9. asterisk-java ami2 事件监听

    asteriskServer文章1提到啦怎么获取,就不解释 asteriskServer.addChainListener(new AsteriskeventListenerInit());//整个服 ...

  10. pycharm使用秘籍 和 pip命令

    python使用requirements.txt批量安装包 requirements.txt文件格式: requests==1.2.0  Flask==0.10.1 等等一系列包 cd 到requir ...