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 ...
随机推荐
- HDU 3001 Travelling (状压DP,3进制)
题意: 给出n<=10个点,有m条边的无向图.问:可以从任意点出发,至多经过同一个点2次,遍历所有点的最小费用? 思路: 本题就是要卡你的内存,由于至多可经过同一个点2次,所以只能用3进制来表示 ...
- codevs 4093 EZ的间谍网络
时间限制: 10 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 由于外国间谍的大量渗入,学校安全正处于高度的危机之中.YJY决定挺身而作出反抗 ...
- LibreOJ #2037. 「SHOI2015」脑洞治疗仪
线段树区间合并问题 恶心... 屠龙宝刀点击就送 #include <cstdio> #define N 200005 struct Segment { int l,r,mid,sum,l ...
- 【Python图像特征的音乐序列生成】关于图像特征的描述词
查阅了很久的资料,决定依据Yoshida的<Image retrieval system using impression words>这篇论文里的词语来定义. Yoshida 等的 Ar ...
- coredata栈
上下文包含所有信息 NSManagedObjectModel The NSManagedObjectModel instance describes the data that is going to ...
- mybatis(一):思维导图
- checkbox绑定v-for的数据
简述自己遇到的问题,觉得合适就拿去用 我在使用v-for遍历checked复选框数据的时候,数据分为两部分,一个主活动,主活动下面有多个子活动 我实体类的设计是里面加个list放子活动, 页面循环需要 ...
- 批量格式化json
单个文件格式化工具: vscode和sublime都有格式化json的插件. 但是笔者遇到的情况是有几百个文件需要格式化,不可能每个文件都打开,进行分别格式化. 于是写了一个python脚本,非常强大 ...
- javaweb基础(20)_JavaBean总结
一.什么是JavaBean JavaBean是一个遵循特定写法的Java类,它通常具有如下特点: 这个Java类必须具有一个无参的构造函数 属性必须私有化. 私有化的属性必须通过public类型的方法 ...
- 【动态规划】bzoj1705: [Usaco2007 Nov]Telephone Wire 架设电话线
可能是一类dp的通用优化 Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设 ...