hdu5441
Travel
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1225 Accepted Submission(s): 443
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?
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.
Note that (a,b) and (b,a) are counted as different pairs and a and b must be different cities.
5 5 3
2 3 6334
1 5 15724
3 5 5705
4 3 12382
1 3 21726
6000
10000
13000
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的更多相关文章
- HDU5441 Travel 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=5441 无向图,n个点,m条带权边,q次询问,给出数值d,点a可以到点b当且仅当存在一条路线其中最大的权值不超过d ...
- hdu5441(2015长春赛区网络赛1005)类最小生成树、并查集
题意:有一张无向图,一些点之间有有权边,某条路径的值等于路径上所有边的边权的最大值,而某个点对的值为这两点间所有路径的值的最小值,给出多个询问,每个询问有一个值,询问有多少点对满足其值小于等于询问值. ...
- HDU5441 Travel (离线操作+并查集)
Travel Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- hdu5441 并查集+克鲁斯卡尔算法
这题计算 一张图上 能走的 点对有多少个 对于每个限制边权 , 对每条边排序,对每个查询排序 然后边做克鲁斯卡尔算法 的时候变计算就好了 #include <iostream> #inc ...
- HDU5441 Travel 离线并查集
Travel Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, ...
- hdu5441 并查集 长春网赛
对于每次询问的大的值,都是从小的值开始的,那就从小到大处理,省去很多时间,并且秩序遍历一遍m; 这题cin容易超时,scanf明显快很多很多.G++又比C++快; //这代码scanf400+,cin ...
随机推荐
- less的使用总结
简单执行less 一.使用npm全局安装less: npm install -g less 二.创建less文件 三.执行命令将less文件转换成css文件 lessc less/style.less ...
- vue.js的package.json相关问题解惑
使用vue-cli创建vue.webpack项目,在项目中用到了iSlider移动端滑动插件,只在本地命令工具中npm install islider.js:提交之后,partner下载代码后一直运行 ...
- HDU Rabbit and Grass 兔子和草 (Nim博弈)
思路:简单Nim博弈,只需要将所给的数字全部进行异或,结果为0,则先手必败.否则必胜. #include <iostream> using namespace std; int main( ...
- 2017.10.2 QBXT 模拟赛
题目链接 T1 我们所要求得是(a*b)|x 也就是 使(a*b)的倍数小于x的个数之和 1<=x<=n 我们可以 找一个c使得 (a*b*c)<=x 由于我们所求的是一个三元有序对 ...
- [学习笔记] C++ 历年试题解析(二)--程序题
发现程序题也挺有价值的. 顺便记录下来几道. 1.题目 #include <iostream> #include <cstring> using namespace ① std ...
- Verilog设计分频器(面试必看)
分频器是指使输出信号频率为输入信号频率整数分之一的电子电路.在许多电子设备中如电子钟.频率合成器等,需要各种不同频率的信号协同工作,常用的方法是以稳定度高的晶体振荡器为主振源,通过变换得到所需要的各种 ...
- SC || Chapter 8
栈:方法调用和局部变量的存储位置,保存基本类型 堆:在一块内存里分为多个小块,每块包含 一个对象,或者未被占用
- Js笔记-第17课
课 // 作业 //深度拷贝 var obj = { name:'rong', age:'25', card:['visa','alipay'], nam :['1','2','3','4','4'] ...
- java第八次作业:课堂上发布的前5张图片(包括匿名对象、单例模式恶汉式、自动生成对象、args[]数组使用、静态关键字)
- Linux常用命令-----------------磁盘挂载命令
磁盘挂载: [root@sdw1 ~]# mkfs.ext4 /dev/vdb[root@sdw1 ~]# blkid /dev/vdb >> /etc/fstabvi /etc/fsta ...