HDU 5441 Travel
Travel
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Description
Input
For each test case, the first line consists of three integers $n, m$ and $q$ where $n \leq 20000, m \leq 100000, q \leq 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 \leq 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
Note that $(a, b)$ and $(b, a)$ are counted as different pairs and $a$ and $b$ must be different cities.
Sample Input
Sample Output
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
using LL = long long;
int uf[maxn];
LL ret,ans[maxn],cnt[maxn];
struct arc {
int u,v,w;
bool operator<(const arc &rhs) const {
return w < rhs.w;
}
} e[];
struct QU {
int w,id;
bool operator<(const QU &rhs) const{
return w < rhs.w;
}
} Q[maxn];
int Find(int x) {
if(x != uf[x]) uf[x] = Find(uf[x]);
return uf[x];
}
bool Union(int x,int y) {
x = Find(x);
y = Find(y);
if(x == y) return false;
ret -= cnt[x]*(cnt[x] - ) + cnt[y]*(cnt[y] - );
ret += (cnt[x] + cnt[y])*(cnt[x] + cnt[y] - );
if(cnt[x] < cnt[y]) {
uf[x] = y;
cnt[y] += cnt[x];
cnt[x] = ;
} else {
uf[y] = x;
cnt[x] += cnt[y];
cnt[y] = ;
}
return true;
}
int main() {
int kase,n,m,q;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d%d",&n,&m,&q);
for(int i = ; i < m; ++i)
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
for(int i = ; i < q; ++i) {
scanf("%d",&Q[i].w);
Q[i].id = i;
}
sort(e,e + m);
sort(Q,Q + q);
for(int i = ; i <= n; ++i) {
uf[i] = i;
cnt[i] = ;
}
ret = ;
for(int i = ,j = ; i < q; ++i) {
while(j < m && e[j].w <= Q[i].w) {
Union(e[j].u,e[j].v);
++j;
}
ans[Q[i].id] = ret;
}
for(int i = ; i < q; ++i)
printf("%I64d\n",ans[i]);
}
return ;
}
HDU 5441 Travel的更多相关文章
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- HDU 5441 Travel(并查集+统计节点个数)
http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...
- hdu 5441 Travel (2015长春网赛)
http://acm.hdu.edu.cn/showproblem.php?pid=5441 题目大意是给一个n个城市(点)m条路线(边)的双向的路线图,每条路线有时间值(带权图),然后q个询问,每个 ...
- hdu 5441 travel 离线+带权并查集
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...
- hdu 5441 Travel(并查集)
Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...
- HDU 5441——Travel——————【并查集+二分查界限】
Travel Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- HDU 5441 Travel (并查集+数学+计数)
题意:给你一个带权的无向图,然后q(q≤5000)次询问,问有多少对城市(城市对(u,v)与(v,u)算不同的城市对,而且u≠v)之间的边的长度不超过d(如果城市u到城市v途经城市w, 那么需要城市u ...
- HDU 5441 Travel (离线dsu)
<题目链接> 题目大意:$n$个点,$m$条边,每条边具有对应的权值,然后进行$k$次询问,每次询问给定一个值,所有权值小于等于这个的边所对应的点能够相连,问每次询问,这些能够相互到达的点 ...
- (并查集)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 ...
随机推荐
- 11.JAVA-Object类之finalize(),clone(),toString()等方法覆写
1.Object介绍 Object类是一个特殊的类,是所有类(包括数组,接口 )的父类,如果一个类没有用extends明确指出继承于某个类,那么它默认继承Object类,所以可以通过向上转型的方法使用 ...
- Apache——DBUtils框架ResultSetHandler接口使用
参考链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 简介 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDB ...
- FreeMusic项目优化(一)——flex布局学习记录
参考博客:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html flex布局是w3c于09年提出的,用于简便,整洁,响应式地解决布局问题的手 ...
- SourceInsight主题设置
自己经常忘记怎样设置SourceInsight主题,这次一定要记住! 0. 退出SourceInsight软件1. 替换配置文件操作:拷贝Global.CF3到“我的文档\Source Insight ...
- linux_base-f10-10_7 linuxulator is not (kld)loaded
# cd linux_base-f10/# make install clean===> linux_base-f10-10_7 linuxulator is not (kld)loaded. ...
- Jenkins结合ant传递参数
需求: 使用Jenkins的「参数化构建过程」,由用户手动输入参数.通过ant脚本接收这个参数,并输出(当然,中间也可以进行复杂的处理,这里为了说明问题,仅做简单的输出). 1.基础环境 Jenkin ...
- 基于eclipse搭建android开发环境-win7 32bit
基于eclipse搭建android开发环境-win7 32bit 前言:在使用朋友已搭建的Android开发环境时,发现朋友的开发环境版本较低且在update SDk时失败,便决定根据网上文章提示从 ...
- hdu 3555 Bomb 炸弹(数位DP,入门)
题意: 给一个数字n,求从1~n中有多少个数是含有49的,比如49,149,1490等都是含49的. 思路: 2^64也顶多是十进制的20多位,那么按十进制位来分析更简单.如果能计算k位十进制数中分别 ...
- 运行powershell 脚本 在此系统上禁止运行脚本
解决方法: 首次在计算机上启动 Windows PowerShell 时,现用执行策略很可能是 Restricted(默认设置). Restricted 策略不允许任何脚本运行. 若要了解计算机上的现 ...
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource
spring boot web项目运行时提示如下错误 org.springframework.beans.factory.BeanCreationException: Error creating b ...