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. SharePoint 2013 安装配置(2)

    上一篇中,我们已经安装了Windows Server 2012 R2.现在第二部分,如何在Windows Server 2012 R2中安装Active Directory域控制器.请按照以下步骤. ...

  2. 洛谷 P2038 无线网络发射器选址

    题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129 条东西向街道和129 条南北向街道所形成的网格状,并且相邻 ...

  3. SpringMVC Logback 设置及使用

    http://b6ec263c.wiz03.com/share/s/2SX2oY0nX4f32CY5ax1bapaL1WPGHe1OeQ-J2ijprB04A67k

  4. [学习总结] python语言学习总结 (三)

    函数闭包 定义 延伸了作用域的函数(能访问定义体之外定义的非全局变量 作用 共享变量的时候避免使用了不安全的全局变量 允许将函数与某些数据关联起来,类似于简化版面向对象编程 相同代码每次生成的闭包,其 ...

  5. 因 URL 意外地以“/HelloWorld”结束,请求格式无法识别。

    web.config文件中的 <system.web> 节点下加入:<webServices>    <protocols>        <add name ...

  6. Spring中c3p0连接池 jar包下载 c3p0-0.9.2.1 jar包和mchange-commons-java-0.2.3.4 jar 包

    c3p0-0.9.2.1 jar包和mchange-commons-java-0.2.3.4 jar 包 下载地址: https://pan.baidu.com/s/1jHDiR7g 密码 tyek

  7. iOS开发-动画总结

    一.简介 IOS 动画主要是指Core Animation框架.官方使用文档地址为:Core Animation Guide.Core Animation是IOS和OS X平台上负责图形渲染与动画的基 ...

  8. xmpp 协议详解

    XMPP(可扩展消息处理现场协议)是基于可扩展标记语言(XML)的协议,它用于即时消息(IM)以及在线现场探测.它在促进服务器之间的准即时操作.这个协议可能最终允许因特网用户向因特网上的其他任何人发送 ...

  9. 【转】C++后台开发应该读的书

    转载自http://www.cnblogs.com/balloonwj/articles/9094905.html 作者 左雪菲 根据我的经验来谈一谈,先介绍一下我的情况,坐标上海,后台开发(也带团队 ...

  10. NOIP模拟赛 高级打字机

    [题目描述] 早苗入手了最新的高级打字机.最新款自然有着与以往不同的功能,那就是它具备撤销功能,厉害吧. 请为这种高级打字机设计一个程序,支持如下3种操作: 1.T x:在文章末尾打下一个小写字母x. ...