Travel

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

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 m bidirectional 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

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const int maxm=;
const int maxq=;
struct node1{
int u,v,w;
node1(){}
node1(int u,int v,int w):u(u),v(v),w(w) {}
}g[maxm]; struct node2{
int d,id;
node2(){}
node2(int d,int id):d(d),id(id) {}
}que[];
long long ans[];
int num[maxn], rak[maxn],father[maxn];
bool cmp1(struct node1 t1,struct node1 t2){
return t1.w<t2.w;
}
bool cmp2(struct node2 t1,struct node2 t2){
return t1.d<t2.d;
} int find(int x){
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
long long tnum;
void Union(int u,int v){
int x=find(u);
int y=find(v);
if(x==y)
return ;
tnum+=num[x]*num[y]; if(rak[x]<rak[y]){
father[x]=y;
num[y]+=num[x];
num[x]=;
}
else {
father[y]=x;
if(rak[x]==rak[y])
++rak[x];
num[x]+=num[y];
num[y]=;
}
} int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m,q; scanf("%d%d%d",&n,&m,&q);
int u,v,w; for(int i=;i<=n;i++){
father[i]=i;
num[i]=;
rak[i]=;
} for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
g[i]=node1(u,v,w);
}
sort(g,g+m,cmp1);
int d;
for(int i=;i<q;i++){
scanf("%d",&d);
que[i]=node2(d,i);
}
sort(que,que+q,cmp2);
memset(ans,,sizeof(ans));
tnum=;
for(int i=,j=;i<q;i++){
int cur=que[i].d;
while(j<m){
node1 temp=g[j]; if(cur>=temp.w){ Union(temp.u,temp.v);
}
else
break;
j++;
}
ans[que[i].id]=tnum;
}
for(int i=;i<q;i++)
printf("%lld\n",ans[i]*); }
return ;
}

hdu5441的更多相关文章

  1. HDU5441 Travel 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 无向图,n个点,m条带权边,q次询问,给出数值d,点a可以到点b当且仅当存在一条路线其中最大的权值不超过d ...

  2. hdu5441(2015长春赛区网络赛1005)类最小生成树、并查集

    题意:有一张无向图,一些点之间有有权边,某条路径的值等于路径上所有边的边权的最大值,而某个点对的值为这两点间所有路径的值的最小值,给出多个询问,每个询问有一个值,询问有多少点对满足其值小于等于询问值. ...

  3. HDU5441 Travel (离线操作+并查集)

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

  4. hdu5441 并查集+克鲁斯卡尔算法

    这题计算 一张图上 能走的 点对有多少个  对于每个限制边权 , 对每条边排序,对每个查询排序 然后边做克鲁斯卡尔算法 的时候变计算就好了 #include <iostream> #inc ...

  5. HDU5441 Travel 离线并查集

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

  6. hdu5441 并查集 长春网赛

    对于每次询问的大的值,都是从小的值开始的,那就从小到大处理,省去很多时间,并且秩序遍历一遍m; 这题cin容易超时,scanf明显快很多很多.G++又比C++快; //这代码scanf400+,cin ...

随机推荐

  1. 【虚拟机-部署】通过 Powershell 来调整 ARM 模式下虚拟机的尺寸

    需求描述 在部署完 ARM 模式的虚拟机以后,可以通过 PowerShell 命令来调整虚拟机的尺寸,以下是通过 PowerShell 命令来调整 ARM 模式的虚拟机尺寸. Note 本文只限于 A ...

  2. SharePoint 2013 安装配置(3-2)

    第三部分SQL Server后端配置篇幅较长,上一篇介绍SQL Server 前提条件及安装(3-1),这篇分享SQL Server功能安装配置,请参考以下步骤. 10.在“安装角色”屏幕上,选择“S ...

  3. POJ 1845 Sumdiv (数学,乘法逆元)

    题意: 给出数字A和B,要求AB的所有因子(包括AB和1)之和 mod 9901 的结果. 思路: 即使知道公式也得推算一阵子. 很容易知道,先把分解得到,那么得到,那么的所有因子之和的表达式如下: ...

  4. Codeforces 786E. ALT 最小割+倍增

    E. ALT http://codeforces.com/problemset/problem/786/E 题意: 给出一棵 n 个节点的树与 m 个工人.每个工人有一条上下班路线(简单路径),一个工 ...

  5. Python:numpy数组转换为json格式

    在python中,如何将一个numpy数组转换为json格式? 这是最近遇到的一个问题,做个笔记. 假设arr为numpy数组,将其转换为json格式: 总体思想是①首先转换为python的list, ...

  6. Array - RemoveDuplicatesfromSortedArray

    /** * 无额外空间,只要前n个是不重复的就行,不需要修改后面的数字 * @param nums 已排序的数组 * @return 去除重复数字后的长度 */ public int removeDu ...

  7. momentum公式

    momentum对于w的更新公式: http://caffe.berkeleyvision.org/tutorial/solver.html

  8. python - 辨识alert、window以及操作

    selenium之 辨识alert.window以及操作 原创 2016年08月24日 11:01:04 4820 0 2 更多关于python selenium的文章,请关注我的专栏:Python ...

  9. PhoneGap+JQuery Mobile移动应用开发学习笔记

    最近一直在学习使用PhoneGap+JQuery Mobile的开发框架开发Android应用,抛开这个框架的运行效率不说,暂且将使用中遇到的问题进行一下整理. 1.JS文件引用顺序 也许在进行web ...

  10. NSCharacterSet去除字符串中的空格、删除指定\任意字符集

    一.去除首尾的空格 /** 1.去除首尾的空格*/ NSString *strMsg=@" 简书作者:CoderZb "; NSString *strResult = [strMs ...